GraphQL Namespace
When projects grow large, code base would be hard to manage and name conflicts happen. In common sense, namespace can be used to solve name conflict, but GraphQL does not support this feature now. There are also proposals to suggest allowing name like “user/create”.
I don’t know when the namespace can be achieved, while under current standard we are still able to get some approaches.
Show Me the Code
The below is an example mutation schema.
To make the schema more organised, we can use nested type to simulate namespace.
In resolvers, userMutation and itemMutation just need return an empty object. It will just take about few minutes to make your code more organised. You even can make a union type for userMutation and itemMutation, like this thread shows.
Front-end
Do we need to worry about namespace in front-end code? The answer is no.
By adding graphql-tag in webpack loaders, the GraphQL schema in front-end are born with namespace.
Let’s have a look on the sample code
Conclusion
In conclusion, nested type can simulate namespace which can make our code more organised. But not all fields can be nested, this approach still cannot entirely replace namespace. I am looking forward the implementation of namespace in GraphQL.
