javascript - How to define recursive GraphQL type programmatically? -
let's say, example, want define typical forum message type using graphql javascript interface:
import { graphqlstring, graphqlobjecttype, } 'graphql'; const messagetype = new graphqlobjecttype({ name: 'message', fields: { text: { type: graphqlstring }, comments: new graphqllist(messagetype), }, }); the javascript compiler (well, interpreter) complain this. messagetype undefined.
we know possible define such type using graphql language:
type message { text: string comments: [message] } how define such type using graphql pure javascript interface?
well, answer simple.
you need pass thunk (function without arguments) field field instead of passing plain javascript object.
the function executed when type defined, so, work.
resulting code looks this:
import { graphqlstring, graphqlobjecttype, } 'graphql'; const messagetype = new graphqlobjecttype({ name: 'message', fields: () => ({ // <-- notice function definition text: { type: graphqlstring }, comments: new graphqllist(messagetype), }), });
Comments
Post a Comment