Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed Jul 11, 2015
0 parents commit c186899
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
9 changes: 9 additions & 0 deletions errors/errors.go
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
}
}
72 changes: 72 additions & 0 deletions executor/executor.go
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
}
40 changes: 40 additions & 0 deletions graphql.go
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
}
}
35 changes: 35 additions & 0 deletions graphql_test.go
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)
}
}
74 changes: 74 additions & 0 deletions language/ast/ast.go
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{}
12 changes: 12 additions & 0 deletions language/parser/parser.go
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
}
16 changes: 16 additions & 0 deletions language/source/source.go
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,
}
}
72 changes: 72 additions & 0 deletions testutil/testutil.go
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,
}
}
46 changes: 46 additions & 0 deletions types/types.go
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 {
}
Loading

0 comments on commit c186899

Please sign in to comment.