question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to specify field type as a string

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sibeliuscommented, Jun 30, 2020

I’m trying to split my types in many packages and it is hard to do using type instances when you can’t avoid circular references in types (most GraphQL schemas that I know).

thunk solves part of this, but it makes it hard to add or remove fields to existing types

you can do something like this

addFields(graphqlObjecttypes, newFields) because it would cause a circular dep to resolve the thunk when adding new fields

we could have a helper to handle both string and graphql object type

export const getObjectType = (typeOrString) => {
   if (typeOrString instanceof GraphQLObjectType) {
     return typeOrString;
   }

    return typeMap[typeOrString];
}
1reaction
stalniycommented, Dec 15, 2020

So, is this something you guys want to see in graphql.js? just don’t want to waste time in case you are against it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modify or change the data type setting for a field
Convert to this type From this type Changes or restrictions Text Memo Access deletes all but the first 255 characters. Number No restrictions. Currency No restrictions....
Read more >
Field data types | Elasticsearch Guide [8.5]
Each field has a field data type, or field type. This type indicates the kind of data the field contains, such as strings...
Read more >
Only allow text field to contain certain string values
I'm new to Prisma - I'm using it with a PostgreSQL database. I have a table in my database that has a string...
Read more >
Changing or converting field type 'string' to 'reference' on ...
"Type change not allowed. Invalid type conversion for field 'assigned_to' on table 'task'. Cannot convert from 'String' to 'Reference'.
Read more >
Custom Field Types
Allows users to enter up to 131,072 characters that display on separate lines similar to a Description field. You can set the length...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found