func schemaWithInputFieldOfType(ttype graphql.Type) (graphql.Schema, error) { badInputObject := graphql.NewInputObject(graphql.InputObjectConfig{ Name: "BadInputObject", Fields: graphql.InputObjectConfigFieldMap{ "badField": &graphql.InputObjectFieldConfig{ Type: ttype, }, }, }) return graphql.NewSchema(graphql.SchemaConfig{ Query: graphql.NewObject(graphql.ObjectConfig{ Name: "Query", Fields: graphql.FieldConfigMap{ "f": &graphql.FieldConfig{ Type: graphql.String, Args: graphql.FieldConfigArgument{ "badArg": &graphql.ArgumentConfig{ Type: badInputObject, }, }, }, }, }), }) }
func TestTypeSystem_InputObjectsMustHaveFields_RejectsAnInputObjectTypeWithMissingFields(t *testing.T) { _, err := schemaWithInputObject(graphql.NewInputObject(graphql.InputObjectConfig{ Name: "SomeInputObject", })) expectedError := "SomeInputObject fields must be an object with field names as keys or a function which return such an object." if err == nil || err.Error() != expectedError { t.Fatalf("Expected error: %v, got %v", expectedError, err) } }
func TestTypeSystem_DefinitionExample_IncludesNestedInputObjectsInTheMap(t *testing.T) { nestedInputObject := graphql.NewInputObject(graphql.InputObjectConfig{ Name: "NestedInputObject", Fields: graphql.InputObjectConfigFieldMap{ "value": &graphql.InputObjectFieldConfig{ Type: graphql.String, }, }, }) someInputObject := graphql.NewInputObject(graphql.InputObjectConfig{ Name: "SomeInputObject", Fields: graphql.InputObjectConfigFieldMap{ "nested": &graphql.InputObjectFieldConfig{ Type: nestedInputObject, }, }, }) someMutation := graphql.NewObject(graphql.ObjectConfig{ Name: "SomeMutation", Fields: graphql.FieldConfigMap{ "mutateSomething": &graphql.FieldConfig{ Type: blogArticle, Args: graphql.FieldConfigArgument{ "input": &graphql.ArgumentConfig{ Type: someInputObject, }, }, }, }, }) schema, err := graphql.NewSchema(graphql.SchemaConfig{ Query: blogQuery, Mutation: someMutation, }) if err != nil { t.Fatalf("unexpected error, got: %v", err) } if schema.GetType("NestedInputObject") != nestedInputObject { t.Fatalf(`schema.GetType("NestedInputObject") expected to equal nestedInputObject, got: %v`, schema.GetType("NestedInputObject")) } }
func TestTypeSystem_InputObjectsMustHaveFields_AcceptsAnInputObjectTypeWithFields(t *testing.T) { _, err := schemaWithInputObject(graphql.NewInputObject(graphql.InputObjectConfig{ Name: "SomeInputObject", Fields: graphql.InputObjectConfigFieldMap{ "f": &graphql.InputObjectFieldConfig{ Type: graphql.String, }, }, })) if err != nil { t.Fatalf("unexpected error: %v", err) } }
if astValue, ok := astValue.(string); ok && astValue == "SerializedValue" { return "DeserializedValue" } return nil }, }) var testInputObject *graphql.InputObject = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "TestInputObject", Fields: graphql.InputObjectConfigFieldMap{ "a": &graphql.InputObjectFieldConfig{ Type: graphql.String, }, "b": &graphql.InputObjectFieldConfig{ Type: graphql.NewList(graphql.String), }, "c": &graphql.InputObjectFieldConfig{ Type: graphql.NewNonNull(graphql.String), }, "d": &graphql.InputObjectFieldConfig{ Type: testComplexScalar, }, }, }) func inputResolved(p graphql.GQLFRParams) interface{} { input, ok := p.Args["input"] if !ok { return nil } b, err := json.Marshal(input)
"f": &graphql.FieldConfig{ Type: graphql.String, }, }, }) var someEnumType = graphql.NewEnum(graphql.EnumConfig{ Name: "SomeEnum", Values: graphql.EnumValueConfigMap{ "ONLY": &graphql.EnumValueConfig{}, }, }) var someInputObject = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "SomeInputObject", Fields: graphql.InputObjectConfigFieldMap{ "f": &graphql.InputObjectFieldConfig{ Type: graphql.String, DefaultValue: "Hello", }, }, }) func withModifiers(ttypes []graphql.Type) []graphql.Type { res := ttypes for _, ttype := range ttypes { res = append(res, graphql.NewList(ttype)) } for _, ttype := range ttypes { res = append(res, graphql.NewNonNull(ttype)) } for _, ttype := range ttypes { res = append(res, graphql.NewNonNull(graphql.NewList(ttype)))
func TestTypeSystem_DefinitionExample_DoesNotMutatePassedFieldDefinitions(t *testing.T) { fields := graphql.FieldConfigMap{ "field1": &graphql.FieldConfig{ Type: graphql.String, }, "field2": &graphql.FieldConfig{ Type: graphql.String, Args: graphql.FieldConfigArgument{ "id": &graphql.ArgumentConfig{ Type: graphql.String, }, }, }, } testObject1 := graphql.NewObject(graphql.ObjectConfig{ Name: "Test1", Fields: fields, }) testObject2 := graphql.NewObject(graphql.ObjectConfig{ Name: "Test2", Fields: fields, }) if !reflect.DeepEqual(testObject1.GetFields(), testObject2.GetFields()) { t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(testObject1.GetFields(), testObject2.GetFields())) } expectedFields := graphql.FieldConfigMap{ "field1": &graphql.FieldConfig{ Type: graphql.String, }, "field2": &graphql.FieldConfig{ Type: graphql.String, Args: graphql.FieldConfigArgument{ "id": &graphql.ArgumentConfig{ Type: graphql.String, }, }, }, } if !reflect.DeepEqual(fields, expectedFields) { t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expectedFields, fields)) } inputFields := graphql.InputObjectConfigFieldMap{ "field1": &graphql.InputObjectFieldConfig{ Type: graphql.String, }, "field2": &graphql.InputObjectFieldConfig{ Type: graphql.String, }, } expectedInputFields := graphql.InputObjectConfigFieldMap{ "field1": &graphql.InputObjectFieldConfig{ Type: graphql.String, }, "field2": &graphql.InputObjectFieldConfig{ Type: graphql.String, }, } testInputObject1 := graphql.NewInputObject(graphql.InputObjectConfig{ Name: "Test1", Fields: inputFields, }) testInputObject2 := graphql.NewInputObject(graphql.InputObjectConfig{ Name: "Test2", Fields: inputFields, }) if !reflect.DeepEqual(testInputObject1.GetFields(), testInputObject2.GetFields()) { t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(testInputObject1.GetFields(), testInputObject2.GetFields())) } if !reflect.DeepEqual(inputFields, expectedInputFields) { t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expectedInputFields, fields)) } }
Name: "Interface", }) var unionType = graphql.NewUnion(graphql.UnionConfig{ Name: "Union", Types: []*graphql.Object{ objectType, }, }) var enumType = graphql.NewEnum(graphql.EnumConfig{ Name: "Enum", Values: graphql.EnumValueConfigMap{ "foo": &graphql.EnumValueConfig{}, }, }) var inputObjectType = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "InputObject", }) func init() { blogAuthor.AddFieldConfig("recentArticle", &graphql.FieldConfig{ Type: blogArticle, }) } func TestTypeSystem_DefinitionExample_DefinesAQueryOnlySchema(t *testing.T) { blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{ Query: blogQuery, }) if err != nil { t.Fatalf("unexpected error, got: %v", err) }
func TestIntrospection_ExecutesAnInputObject(t *testing.T) { testInputObject := graphql.NewInputObject(graphql.InputObjectConfig{ Name: "TestInputObject", Fields: graphql.InputObjectConfigFieldMap{ "a": &graphql.InputObjectFieldConfig{ Type: graphql.String, DefaultValue: "foo", }, "b": &graphql.InputObjectFieldConfig{ Type: graphql.NewList(graphql.String), }, }, }) testType := graphql.NewObject(graphql.ObjectConfig{ Name: "TestType", Fields: graphql.FieldConfigMap{ "field": &graphql.FieldConfig{ Type: graphql.String, Args: graphql.FieldConfigArgument{ "complex": &graphql.ArgumentConfig{ Type: testInputObject, }, }, Resolve: func(p graphql.GQLFRParams) interface{} { return p.Args["complex"] }, }, }, }) schema, err := graphql.NewSchema(graphql.SchemaConfig{ Query: testType, }) if err != nil { t.Fatalf("Error creating Schema: %v", err.Error()) } query := ` { __schema { types { kind name inputFields { name type { ...TypeRef } defaultValue } } } } fragment TypeRef on __Type { kind name ofType { kind name ofType { kind name ofType { kind name } } } } ` expectedDataSubSet := map[string]interface{}{ "__schema": map[string]interface{}{ "types": []interface{}{ map[string]interface{}{ "kind": "INPUT_OBJECT", "name": "TestInputObject", "inputFields": []interface{}{ map[string]interface{}{ "name": "a", "type": map[string]interface{}{ "kind": "SCALAR", "name": "String", "ofType": nil, }, "defaultValue": `"foo"`, }, map[string]interface{}{ "name": "b", "type": map[string]interface{}{ "kind": "LIST", "name": nil, "ofType": map[string]interface{}{ "kind": "SCALAR", "name": "String", "ofType": nil, }, }, "defaultValue": nil, }, }, }, }, }, } result := g(t, graphql.Params{ Schema: schema, RequestString: query, }) if !testutil.ContainSubset(result.Data.(map[string]interface{}), expectedDataSubSet) { t.Fatalf("unexpected, result does not contain subset of expected data") } }