πŸ‘ Types and interfaces in GraphQL by PoP are now namespaced!

β€” 5 minute read

I have just made the types and interfaces in GraphQL by PoP be automatically namespaced!

This is how the normal schema looks like in the GraphQL Voyager:

Interactive schema "Interactive schema"

This is how it looks in its namespaced version:

Namespaced interactive schema "Namespaced interactive schema"

In the namespaced schema, all types are automatically prepended using the PHP package's owner and name (in this case it is PoP_ComponentModel_, where PoP is the package owner, and ComponentModel is the package name).

Why namespaces permalink

Namespaces help manage the complexity of the schema. This is particularly useful when embedding components from a 3rd party, where we can't control how the types have been named. For instance, different plugins in WordPress may implement a Product custom post type (such as WooCommerce or Easy Digital Downloads); if they wish to create a GraphQL type for it, they can't just name it Product or it may clash with another plugin. Hence, they would have to manually prepend their their type names with the company name (such as doing WooCommerce_Product), which is not the most beautiful solution.

Now, GraphQL by PoP enables to define an environment variable, and it will automatically prepend all types from a package with the PHP namespace used for that package (following the PSR-4 convention, PHP namespaces have the form of ownerName\projectName, such as "PoP\ComponentModel").

There are many more use cases where namespaces can be pretty useful, listed down in this GitHub issue.

GraphiQL Demo! permalink

If you want to play with the namespaced schema, you can do it in the GraphiQL clients below.

This is the GraphiQL client in normal mode:

An this is the GraphiQL client in namespaced mode (which is enabled by adding URL param use_namespace=1):

Coming next... πŸ₯ permalink

I'll end my blog post as usual: if you are using WordPress and you need a kick-ass API, then give GraphQL by PoP a try, what are you waiting for!?

Oh, you're waiting for the plugin, you say?

Well, good news then... it is coming sooooon...

This is going to be gooooood stuff, I promise! πŸ€ͺ