Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate .graphql schema file #378

Closed
Aleksion opened this issue Aug 26, 2017 · 8 comments
Closed

Generate .graphql schema file #378

Aleksion opened this issue Aug 26, 2017 · 8 comments

Comments

@Aleksion
Copy link

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.

@benwilson512
Copy link
Contributor

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?

@Aleksion
Copy link
Author

Aleksion commented Aug 26, 2017

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.

@bruce
Copy link
Contributor

bruce commented Sep 1, 2017

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).

@dlindenkreuz
Copy link

@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.

@Aleksion
Copy link
Author

Aleksion commented Nov 2, 2017

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

@AaronJamesKing
Copy link

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!

@bruce
Copy link
Contributor

bruce commented Feb 22, 2018

Here’s 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

@maartenvanvliet
Copy link
Contributor

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants