Enforce one field based on another - kebab-case example

docs PR: https://github.com/dgraph-io/dgraph-docs/pull/127

A resolver example using a graphql call and manually overriding the authHeader

provided by the client:

async function secretGraphQL({ parents, graphql }) {
 const ids = parents.map((p) => p.id);
 const secretResults = await graphql(
   `query myQueryName ($ids: [ID!]) { queryMyType(filter: { id: $ids }) { id controlledEdge { myField } }> }`,
   { ids },
   {
     key: 'X-My-App-Auth'
     value: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwczovL215LmFwcC5pby9qd3QvY2xhaW1zIjp7IlVTRVIiOiJmb28ifSwiZXhwIjoxODAwMDAwMDAwLCJzdWIiOiJ0ZXN0IiwibmFtZSI6IkpvaG4gRG9lIDIiLCJpYXQiOjE1MTYyMzkwMjJ9.wI3857KzwjtZAtOjng6MnzKVhFSqS1vt1SjxUMZF4jc'
   }
 );
 return parents.map((parent) => {
   const secretRes = secretResults.data.find(res => res.id === parent.id)
   parent.secretProperty = null
   if (secretRes) {
     if (secretRes.controlledEdge) {
       parent.secretProperty = secretRes.controlledEdge.myField
     }
   }
   return parent
 });
}
self.addMultiParentGraphQLResolvers({
 "MyType.secretProperty": secretGraphQL,
});

The above is an example of how to pass a JWT. How to make a JWT really depends on your auth setup. If you are using a 3rd party provider for auth such as Auth0, then you should do a login over http to the 3rd party. If you are using a more static configuration, then you could generate the jwt within the lambda script. But keep in mind that if you need to use any 3rd party packages, you will need to compile the script with webpack before pushing to Dgraph Cloud Lambda.

1 Like