Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular references causes compiler error #164

Closed
rubberviscous opened this issue Oct 11, 2016 · 4 comments
Closed

Circular references causes compiler error #164

rubberviscous opened this issue Oct 11, 2016 · 4 comments
Labels

Comments

@rubberviscous
Copy link

rubberviscous commented Oct 11, 2016

When defining GraphQL types where two types reference each other, this causes compiler error when building:

typechecking loop involving showType = graphql.NewObject(graphql.ObjectConfig literal)

Here's the GraphQL types that I have defined where it references each other:

var showType = graphql.NewObject( graphql.ObjectConfig{ Name: "Show", Fields: graphql.Fields{ "id": &graphql.Field{ Type: graphql.String, }, "episodes": &graphql.Field{ Type: graphql.NewList(episodeType), } } } )

var episodeType = graphql.NewObject( graphql.ObjectConfig{ Name: "Episode", Fields: graphql.Fields{ "id": &graphql.Field{ Type: graphql.String, }, "show": &graphql.Field{ Type: showType, } } )

Are there any workarounds for this issue?

@sfriedel
Copy link
Contributor

@rubberviscous I also had this problem once. My solution was dynamically adding one of the problematic fields inside init(), It's not really pretty but it seems work at least

package main

import (
    "github.com/graphql-go/graphql"
)

var showType = graphql.NewObject(graphql.ObjectConfig{
    Name: "Show",
    Fields: graphql.Fields{
        "id":       &graphql.Field{Type: graphql.String},
        "episodes": &graphql.Field{Type: graphql.NewList(episodeType)},
    },
})

var episodeType = graphql.NewObject(graphql.ObjectConfig{
    Name: "Episode",
    Fields: graphql.Fields{
        "id": &graphql.Field{Type: graphql.String},
    },
})

func init() {
    episodeType.AddFieldConfig("show", &graphql.Field{Type: showType})
}

@rubberviscous
Copy link
Author

@sfriedel That worked! Many thanks!

@chris-ramon
Copy link
Member

Hi @rubberviscous, thanks a lot for reaching us for a question, also the solution @sfriedel wrote above is the go-to one, closing this one, thanks guys!

@johndww
Copy link

johndww commented May 18, 2018

I have the same issue here, but for InputObject. I tried the same workaround, but there is no AddFieldConfig defined for InputObject. I've done the following hack but I'm worried about it being broken in a subsequent commit:

func init() {
	// this is a bit hacky. there is no AddFieldConfig() equivalent for input types so we need to force
	// the input field mapping initialization, and then directly assign the new field
	GraphQLInputType.Fields()

	GraphQLInputType.Fields()["children"] = &graphql.InputObjectField{
		PrivateName: "children",
		Type: graphql.NewList(GraphQLInputType),
	}
}

Any other suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants