graphql / graphql/graphql-js

Allow to specify field type as a string

Open
#1,979 12 comments 2 reactions 0 assignees View on GitHub
needs exploration
Dominant language
TypeScript
Stars
20.3k
Forks
2.1k
Avg merge
44m
Merged PRs (30d)
6

Description

## Currently

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

```js
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:

```js
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:

```js
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:

```js
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.