Dgraph Directives reference (for other tools)?

I have a dgraph-friendly gql schema that works great there. I’d like to use other tools also, though, and the directives are misunderstood. Specifically:

I’m working on a multi-tenant app in go. I have the official dgo client running for the app-to-data connection, and that’s gold. For the user-to-app connection, I’m standing-up a separate, user-session-aware gql server (effectively using gqlgen as a gql proxy) that will carry extra logic depending on things that have no place in dgraph.

obv, it would be better to maintain a single schema, but gqlgen generate fails miserably because of the dgraph directives. I’ve been polyfilling and stubbing, trying to get a clean build, but I think I’m spinning my wheels.

Is there a library or something similar that will add the dgraph directives to my local pipeline?

Where I’m at:

type FooUser {
    username: String! @id @search(by:[hash])
    name: String @search(by:[exact])
    things: [FooThing!]!
}
type FooThing{
    id: ID!
    title: String! @search(by:[hash, fulltext])
}

type EnumValue{
    stub: Boolean
}
union Searchable = EnumValue
directive @search(by: [Searchable]) on FIELD_DEFINITION|OBJECT
directive @id on FIELD_DEFINITION
directive @hasInverse(field: EnumValue) on FIELD_DEFINITION

where I’d like to be:

type FooUser{
  //... 
}
type FooThing{
  //...
}

As I understand the question, you need a way to automatically generate definitions of directives which you are using in Dgraph GraphQL schema so that the schema (along with directive definitions) could be used with gqlgen.

Dgraph GraphQL takes in a schema, adds queries, mutations, and definitions of directives and other extra things to generate a schema which could then be used with gqlgen.

You may find examples of schema generated by Dgraph GraphQL for every schema in input directory in the output directory of dgraph/graphql/schema/testdata/schemagen at master · dgraph-io/dgraph · GitHub .

The generated GraphQL schema can be queried using the following query on /admin endpoint.

query{
  getGQLSchema{
    generatedSchema
  }
}

I hope that this answers your question. Do let us know if you have any more questions.

That’s right, there IS an endpoint that returns the “clean” gql! I can just pass that to external tools and they’ll understand.

Thank you, I think that’ll do it.

This is just a job for a codegen pipeline :slight_smile:

1 Like

Since this is indexed and others may come for a similar issue:

The final solution that seems to work really well is below.
My specific case is using gqlgen to generate gql server stub from the active dgraph gql schema, but the same idea should work just as well for any other ci/cd/auto pipelines.

The curl command uses the recommendation from @rajas, and just adds python/jq for clean pipe-to-schema-file.

I translated my Taskfile to bash, so please don’t copy-paste.

# routine cleanup
rm -f gql-srv/graph/schema.graphqls

# get generated schema from /admin endpoint.
#  use python and jq to pluck .data.getGQLSchema.generatedSchema from response and pass to schema file
curl 'http://localhost:8080/admin' \
    -H 'Content-Type: application/json' \
    --data-binary '{"query":"query{getGQLSchema{generatedSchema}}"}' | 
    python -m json.tool | jq -r '.data.getGQLSchema.generatedSchema' \
        > gql-srv/graph/schema.graphqls

# regenerate gql server stub from updated .graphqls
cd gql-srv && gqlgen generate
2 Likes