Absinthe: Generate .graphql schema file

Created on 26 Aug 2017  路  8Comments  路  Source: absinthe-graphql/absinthe

Hi,

Besides generating the introspection result is it possible to generate a .graphql schema?
Some of the tooling I usually work with with graphql requires .graphql schema to operate properly. There are some outdated js tools to convert from the introspection result, but it would be much nicer to do it directly from elixir.

Feature

Most helpful comment

There is a mix task now to generate the SDL 馃帀. See https://hexdocs.pm/absinthe/1.5.0-beta.2/Mix.Tasks.Absinthe.Schema.Sdl.html#content

This issue can be closed :)

All 8 comments

Hey! It's definitely on the roadmap, although everything we've run into so far has been usable with the output from https://hexdocs.pm/absinthe/Mix.Tasks.Absinthe.Schema.Json.html

Does that task not work for some of your tools?

I'm pretty fond of VSCode and the current plugin for graphql. https://github.com/kumarharsh/graphql-for-vscode uses a service that only works with .graphql files.

The better solution is to create a plugin that implements Facebooks Own reference language server. https://github.com/graphql/graphql-language-service/issues/7
But it currently isn't seeing much progress (I'll try to get around to it when I'm not racing against deadlines :D)

But it might still make sense to be able to output .graphql files to accommodate future use cases.

Yeah, this is a roadmap item for (currently slated) v1.5, when using a .graphql (IDL) to _define_ a schema is also planned (in addition to the existing macro approach).

@Aleksion just curious, do you have any issues with https://github.com/apollographql/apollo-codegen? I get along quite well with apollo-codegen and absinthe.

Nope, Codegen works just fine. The problem has been my graphql vscode plugin. I'm running a script to convert it currently, so my plugin works (using the official javascript library)

cp "../MY_ELIXIR_APP/schema.json" "./src/app/schema.json"


 gql-gen --file ./src/app/schema.json --template typescript --out ./src/app/queries/schema.d.ts "./src/**/*.ts"
 node ./bash/graphql.js --src "./src/app/schema.json" --out "./src/app/schema.graphql"

and then the javascript file:

const { buildSchema, buildClientSchema, printSchema, introspectionQuery } = require("graphql")
const fs = require("fs")


let src = null
let out = null

process.argv.forEach(function (val, index, array) {
    if (val === "src") {
        src = array[index + 1]
    }
    switch (val) {
        case "--src":
            src = array[index + 1]
            break;
        case "--out":
            out = array[index + 1]
            break
    }
});


if (!src) {
    console.error("must provide src path")
    return
}

if (!out) {
    console.error("must provide out path")
    return
}


function writeSchema(src, out) {
    const fileContent = fs.readFileSync(src, "utf-8")

    if (!fileContent) {
        console.error("no schema found")
        return
    }

    const { data } = JSON.parse(fileContent)

    const clientSchema = buildClientSchema(data)

    const graphqlSchemaString = printSchema(clientSchema)

    fs.writeFileSync(out, graphqlSchemaString)
}

writeSchema(src, out)

I'm using gql-gen though, but apollo codegen worked fine as far as I remember

A .graphql schema would also be useful for the following scenario:
I'm developing a frontend with Apollo, and using graphql-tools's makeExecutableSchema and addMockFunctionsToSchema to build a rudimentary mock server. But makeExecutableSchema only takes in GQL Schema Language strings to build the schema. It would be really awesome if I could export my server's schema right into the frontend's mock server!

Here鈥檚 a useful tool to get an SDL (.graphql) representation of your schema via converting the result of introspection; this might help until SDL export (& import) support arrives in Absinthe v1.5: https://github.com/graphcool/get-graphql-schema

There is a mix task now to generate the SDL 馃帀. See https://hexdocs.pm/absinthe/1.5.0-beta.2/Mix.Tasks.Absinthe.Schema.Sdl.html#content

This issue can be closed :)

Was this page helpful?
0 / 5 - 0 ratings