graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.82k stars 836 forks source link

Create graphql schema from string #662

Closed shohamroditimemphis closed 1 year ago

shohamroditimemphis commented 1 year ago

There is an option to create *graphql.Schema from string. This is my string : `type Query { greeting:String students:[Student] }

     type Student {
        id:ID!
        firstName:String
        lastName:String
        password:String
        collegeId:String
     }

` and I want to create from string *graphql.Schema

bhoriuchi commented 1 year ago

You can use this project I maintain to accomplish this https://github.com/bhoriuchi/graphql-go-tools

pavelnikolov commented 1 year ago

@shohamroditimemphis You can use https://github.com/graph-gophers/graphql-go The Schema.ASTSchema() method does exactly what you want:

package main

import (
    "fmt"

    "github.com/graph-gophers/graphql-go"
)

func main() {
    s := `
        query {
            translate(text: String!, from: Language!, to: Language!) String!
        }

        enum Language {
            ENGLISH
            FRENCH
            GERMAN
            SPANISH
        }
    `
    schema := graphql.MustParseSchema(s, nil)
    ast := schema.ASTSchema()

    for _, e := range ast.Enums {
        fmt.Printf("Enum %q has the following options:\n", e.Name)
        for _, o := range e.EnumValuesDefinition {
            fmt.Printf("  - %s\n", o.EnumValue)
        }
    }
    // output:
    // Enum "Language" has the following options:
    //   - ENGLISH
    //   - FRENCH
    //   - GERMAN
    //   - SPANISH
}
shohamroditimemphis commented 1 year ago

 Thanks