Skip to content

Allow to specify field type as a string #1979

@stalniy

Description

@stalniy

Currently

graphql.js expects type option to be an instance of GraphQLType (GraphQLInputObjectType, GraphQLObjectType, etc).
Example:

const AnotherType = new GraphQLObjectType({ ... })
const Type = new GraphQLObjectType({
  name: 'MyType',
  fields: {
    anotherType: { type: AnotherType }
  }
})

Desired

In order to split application into modules (which may have circular references in GraphQL types) it'd be helpful to be able to specify type parameter in fields as a string. Updated example:

const Type = new GraphQLObjectType({
  name: 'MyType',
  fields: {
    anotherType: { type: 'AnotherType' } // AnotherType is referenced by its name
  }
})

Workaround

Currently when nodejs modules have circular reference, I can use ugly workaround: calling require in fields thunk:

const Type = new GraphQLObjectType({
  name: 'MyType',
  fields: () => ({
    anotherType: { type: require('anotherModule').AnotherType } 
  })
})

Implementation details

We can add a restriction, that types which a referenced by their name must be included in types option of GraphQLSchema constructor:

const schema = new GraphQLSchema({
  types: [/* specify types that were referenced by their name */],
  query: ...,
  mutation: ...
  subscription: ...
})

So, then the flow is this:

  1. Walk over types array, add them into typeMap
  2. Walk over Query, Mutation, Subscription recursively and resolve types

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions