GraphQL Namespace

Yu Huang
Yu Huang
Sep 9, 2018 · 1 min read

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.

Yu Huang
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade