Skip to content

edcolyer/graphql-api

Repository files navigation

GraphQL Experimentation

Just playing around and learning GraphQL for APIs.

Some Notes

Querying in development

I was using GraphQL Playground for querying the GraphQL server initially, however express-graphql includes a Graphiql editor when you set graphiql: true. You just need to visit localhost:4000/graphql to view this.

When passing arguments in code

It is best to pass arguments using $ syntax with JSON.stringify like the example here. The GraphQL implementation will escape this so we don't have to worry about escaping on the client side.

Return what you mutate

When the API mutates an object, it is certainly best practice to return the result of what has been mutated so that the client can learn about what it has modified. A side effect of this is that it also makes integration testing a lot easier.

Input types

Commonly creating and updating take the same parameters. In this situation we can use the input keyword as per here to create "input types".

The ID type

The GraphQL ID type represents a unique ID for an object. It is parsed as a string. E.g. in the case of an ID 127, it would be parsed as "127". This is discussed in this Stack Overflow post.

Layers of functionality

Separate entry points (e.g. GraphQL) from auth, business logic, and persistence (obviously). See here for a great illustration.

Pagination

Whilst GraphQL doesn't explicitly specify how pagination should be done, there are some common practices. For a great design pattern, go here.

Less Related Notes

Destructuring function parameters

I really like this syntax for destructuring parameters from objects in functions that take an object as the sole argument:

  function fooBar({ foo, bar }) {
    console.log('Foo:', foo);
    console.log('Bar:', bar);
  }
Knex

Knex is pretty cool for SQL queries. I definitely see merit in it for building queries dynamically. I think pg-promise is easier to use for executing queries, transactions, and so on, however. I also think there is merit in dealing with pure SQL in lots of situations, but using something like this ES6 SQL template tag would be useful. A combination of Knex and pg-promise, with SQL template tag, would be ideal.

About

Experimenting with GraphQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published