How to configure Graphene-Django to work with persisted queries?

狂风中的少年 提交于 2019-12-03 18:22:20

Persisted queries are not a part of the GraphQL spec and, as such, can be implemented in a wide variety of ways. Here are a few examples of how you might want to do this on your server:

Extract Queries

As of the time of this writing, you can extract static queries with Relay Modern, the Apolo Client, and others. They all work in similar ways, so I'll use Apollo PersistGraphQL as an example. In your build, you will need to run the persistgraphql command over your src directory to extract your static queries. The result of this command will be a JSON file filled with queries, as strings, and a number as the value.

{
  "
  { 
    author {
      firstName
      lastName
    }
  }
  ": 9,
  "
  query otherQuery {
    person {
      firstName
      lastName
    }
  }
  ": 10
}

Use extracted queries

From here, you have a few options. Once your server is aware of all possible queries, it can either provide an interface to the values provided in the JSON file, or it can whitelist the queries it knows about. If your server only provides an interface to the values (myserver/api/9, myserver/api/10 in the example above), you would need to make sure that your client app maps it's queries to those agreed on IDs by having it consume the same JSON file. Alternatively, you could use that file to prevent unexpected queries from being executed without modifying the client in any way.

How you specifically set up your server to consume this JSON file is up to you. Some people will pre-execute the set of known queries and put them into a fast data-store like Redis. Some people use it strictly for preventing unauthorized queries. As far as how this is done with Django-Graphene, there is no out-of-the-box solution that I'm aware of but consuming an extracted key/value store like the one above should provide your team with a bunch of good options.

You can check Persisted queries for Graphene Django

https://github.com/flavors/django-graphql-persist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!