forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c186899
Showing
11 changed files
with
393 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package errors | ||
|
||
type GraphQLFormattedError struct { | ||
Message string | ||
Locations []struct { | ||
Line int | ||
Column int | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package executor | ||
|
||
import ( | ||
"github.com/chris-ramon/graphql-go/errors" | ||
"github.com/chris-ramon/graphql-go/language/ast" | ||
"github.com/chris-ramon/graphql-go/types" | ||
) | ||
|
||
type ExecutionResult struct { | ||
Data interface{} | ||
Errors []errors.GraphQLFormattedError | ||
} | ||
|
||
type ExecuteParams struct { | ||
Schema types.GraphQLSchema | ||
Root map[string]interface{} | ||
AST ast.Document | ||
OperationName string | ||
Args map[string]string | ||
} | ||
|
||
func Execute(p ExecuteParams, r chan types.GraphQLResult) { | ||
var errors []error | ||
params := BuildExecutionCtxParams{ | ||
Schema: p.Schema, | ||
Root: p.Root, | ||
AST: p.AST, | ||
OperationName: p.OperationName, | ||
Args: p.Args, | ||
Errors: errors, | ||
} | ||
exeContext := buildExecutionContext(params) | ||
eOperationParams := ExecuteOperationParams{ | ||
ExecutionContext: exeContext, | ||
Root: p.Root, | ||
Operation: exeContext.Operation, | ||
} | ||
executeOperation(eOperationParams, r) | ||
} | ||
|
||
func executeOperation(p ExecuteOperationParams, r chan types.GraphQLResult) { | ||
var result types.GraphQLResult | ||
r <- result | ||
} | ||
|
||
type ExecutionContext struct { | ||
Schema types.GraphQLSchema | ||
Fragments map[string]ast.FragmentDefinition | ||
Root map[string]interface{} | ||
Operation ast.OperationDefinition | ||
Variables map[string]interface{} | ||
Errors []error | ||
} | ||
|
||
type BuildExecutionCtxParams struct { | ||
Schema types.GraphQLSchema | ||
Root map[string]interface{} | ||
AST ast.Document | ||
OperationName string | ||
Args map[string]string | ||
Errors []error | ||
} | ||
|
||
type ExecuteOperationParams struct { | ||
ExecutionContext ExecutionContext | ||
Root map[string]interface{} | ||
Operation ast.OperationDefinition | ||
} | ||
|
||
func buildExecutionContext(p BuildExecutionCtxParams) (eCtx ExecutionContext) { | ||
return eCtx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gql | ||
|
||
import ( | ||
"github.com/chris-ramon/graphql-go/executor" | ||
"github.com/chris-ramon/graphql-go/language/parser" | ||
"github.com/chris-ramon/graphql-go/language/source" | ||
"github.com/chris-ramon/graphql-go/types" | ||
"github.com/chris-ramon/graphql-go/validator" | ||
) | ||
|
||
type GraphqlParams struct { | ||
Schema types.GraphQLSchema | ||
RequestString string | ||
RootObject map[string]interface{} | ||
VariableValues map[string]string | ||
OperationName string | ||
} | ||
|
||
func Graphql(p GraphqlParams, resultChannel chan types.GraphQLResult) { | ||
source := source.NewSource(p.RequestString, "GraphQL request") | ||
AST := parser.Parse(source, parser.ParseOptions{}) | ||
validationResult := validator.ValidateDocument(p.Schema, AST) | ||
if !validationResult.IsValid { | ||
result := types.GraphQLResult{ | ||
Errors: validationResult.Errors, | ||
} | ||
resultChannel <- result | ||
return | ||
} else { | ||
ep := executor.ExecuteParams{ | ||
Schema: p.Schema, | ||
Root: p.RootObject, | ||
AST: AST, | ||
OperationName: p.OperationName, | ||
Args: p.VariableValues, | ||
} | ||
executor.Execute(ep, resultChannel) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package gql | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/chris-ramon/graphql-go/types" | ||
|
||
"./testutil" | ||
) | ||
|
||
func TestQuery(t *testing.T) { | ||
var query string | ||
query = ` | ||
query HeroNameQuery { | ||
hero { | ||
name | ||
} | ||
} | ||
` | ||
expected := testutil.StarWarsChar{ | ||
Name: "R2-D2", | ||
} | ||
graphqlParams := GraphqlParams{ | ||
Schema: testutil.StarWarsSchema, | ||
RequestString: query, | ||
} | ||
resultChannel := make(chan types.GraphQLResult) | ||
Graphql(graphqlParams, resultChannel) | ||
graphqlResult := <-resultChannel | ||
hero := graphqlResult.Data["hero"].(map[string]interface{}) | ||
close(resultChannel) | ||
if expected.Name != hero["name"] { | ||
t.Errorf("wrong result, query: %v, graphql result: %v", query, graphqlResult) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ast | ||
|
||
import "github.com/chris-ramon/graphql-go/language/source" | ||
|
||
type Definition interface { | ||
} | ||
|
||
type Name struct { | ||
Kind string | ||
Loc Location | ||
Value string | ||
} | ||
|
||
func NewName() *Name { | ||
return &Name{ | ||
Kind: "Name", | ||
} | ||
} | ||
|
||
type SelectionSet struct { | ||
Kind string | ||
Location Location | ||
Selections []interface{} | ||
} | ||
|
||
func NewSelectionSet() *SelectionSet { | ||
return &SelectionSet{ | ||
Kind: "SelectionSet", | ||
} | ||
} | ||
|
||
type Value interface{} | ||
|
||
type Directive struct { | ||
Kind string | ||
Loc Location | ||
Name Name | ||
Value Value | ||
} | ||
|
||
func NewDirective() *Directive { | ||
return &Directive{ | ||
Kind: "Directive", | ||
} | ||
} | ||
|
||
type FragmentDefinition struct { | ||
Kind string | ||
Loc Location | ||
Name Name | ||
TypeCondition Name | ||
Directives []Directive | ||
SelectionSet SelectionSet | ||
} | ||
|
||
func NewFragmentDefinition() *FragmentDefinition { | ||
return &FragmentDefinition{ | ||
Kind: "FragmentDefinition", | ||
} | ||
} | ||
|
||
type Location struct { | ||
Start int | ||
End int | ||
Source source.Source | ||
} | ||
|
||
type Document struct { | ||
Kind string | ||
Loc Location | ||
Definitions []Definition | ||
} | ||
|
||
type OperationDefinition interface{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package parser | ||
|
||
import "github.com/chris-ramon/graphql-go/language/ast" | ||
|
||
type ParseOptions struct { | ||
NoLocation bool | ||
NoSource bool | ||
} | ||
|
||
func Parse(source interface{}, options ParseOptions) (doc ast.Document) { | ||
return doc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package source | ||
|
||
type Source struct { | ||
Body string | ||
Name string | ||
} | ||
|
||
func NewSource(body, name string) *Source { | ||
if name == "" { | ||
name = "GraphQL" | ||
} | ||
return &Source{ | ||
Body: body, | ||
Name: name, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package testutil | ||
|
||
import "github.com/chris-ramon/graphql-go/types" | ||
|
||
var ( | ||
Luke StarWarsChar | ||
Vader StarWarsChar | ||
Han StarWarsChar | ||
Leia StarWarsChar | ||
Tarkin StarWarsChar | ||
HumanData map[int]StarWarsChar | ||
StarWarsSchema types.GraphQLSchema | ||
) | ||
|
||
type StarWarsChar struct { | ||
Id string | ||
Name string | ||
Friends []string | ||
AppearsIn []int | ||
HomePlanet string | ||
} | ||
|
||
func init() { | ||
Luke = StarWarsChar{ | ||
Id: "1000", | ||
Name: "Luke Skywalker", | ||
Friends: []string{"1002", "1003", "2000", "2001"}, | ||
AppearsIn: []int{4, 5, 6}, | ||
HomePlanet: "Tatooine", | ||
} | ||
Vader = StarWarsChar{ | ||
Id: "1001", | ||
Name: "Darth Vader", | ||
Friends: []string{"1004"}, | ||
AppearsIn: []int{4, 5, 6}, | ||
HomePlanet: "Tatooine", | ||
} | ||
Han = StarWarsChar{ | ||
Id: "1002", | ||
Name: "Han Solo", | ||
Friends: []string{"1000", "1003", "2001"}, | ||
AppearsIn: []int{4, 5, 6}, | ||
} | ||
Leia = StarWarsChar{ | ||
Id: "1003", | ||
Name: "Leia Organa", | ||
Friends: []string{"1000", "1002", "2000", "2001"}, | ||
AppearsIn: []int{4, 5, 6}, | ||
HomePlanet: "Alderaa", | ||
} | ||
Tarkin = StarWarsChar{ | ||
Id: "1004", | ||
Name: "Wilhuff Tarkin", | ||
Friends: []string{"1001"}, | ||
AppearsIn: []int{4}, | ||
} | ||
HumanData = map[int]StarWarsChar{ | ||
1000: Luke, | ||
1001: Vader, | ||
1002: Han, | ||
1003: Leia, | ||
1004: Tarkin, | ||
} | ||
var fields types.GraphQLObjectTypeFields | ||
queryType := types.GraphQLObjectType{ | ||
Name: "Query", | ||
Fields: fields, | ||
} | ||
StarWarsSchema = types.GraphQLSchema{ | ||
Query: queryType, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package types | ||
|
||
import "github.com/chris-ramon/graphql-go/errors" | ||
|
||
type Schema interface{} | ||
|
||
type GraphQLResult struct { | ||
Data map[string]interface{} | ||
Errors []errors.GraphQLFormattedError | ||
} | ||
|
||
type GraphQLEnumType struct { | ||
} | ||
|
||
type GraphQLInterfaceType struct { | ||
} | ||
|
||
type GraphQLObjectTypeFields func() | ||
|
||
type GraphQLObjectType struct { | ||
Name string | ||
Fields GraphQLObjectTypeFields | ||
} | ||
|
||
type GraphQLList struct { | ||
} | ||
|
||
type GraphQLNonNull struct { | ||
} | ||
|
||
type GraphQLSchemaConfig struct { | ||
query GraphQLObjectType | ||
mutation GraphQLObjectType | ||
} | ||
|
||
type GraphQLSchema struct { | ||
Query GraphQLObjectType | ||
schemaConfig GraphQLSchemaConfig | ||
} | ||
|
||
func (gq *GraphQLSchema) Constructor(config GraphQLSchemaConfig) { | ||
gq.schemaConfig = config | ||
} | ||
|
||
type GraphQLString struct { | ||
} |
Oops, something went wrong.