func TestTypeSystem_SchemaMustContainUniquelyNamedTypes_RejectsASchemaWhichRedefinesABuiltInType(t *testing.T) { fakeString := graphql.NewScalar(graphql.ScalarConfig{ Name: "String", Serialize: func(value interface{}) interface{} { return nil }, }) queryType := graphql.NewObject(graphql.ObjectConfig{ Name: "Query", Fields: graphql.FieldConfigMap{ "normal": &graphql.FieldConfig{ Type: graphql.String, }, "fake": &graphql.FieldConfig{ Type: fakeString, }, }, }) _, err := graphql.NewSchema(graphql.SchemaConfig{ Query: queryType, }) expectedError := `Schema must contain unique named types but contains multiple types named "String".` if err == nil || err.Error() != expectedError { t.Fatalf("Expected error: %v, got %v", expectedError, err) } }
func TestTypeSystem_ScalarTypesMustBeSerializable_RejectsAScalarTypeNotDefiningSerialize(t *testing.T) { _, err := schemaWithFieldType(graphql.NewScalar(graphql.ScalarConfig{ Name: "SomeScalar", })) expectedError := `SomeScalar must provide "serialize" function. If this custom Scalar ` + `is also used as an input type, ensure "parseValue" and "parseLiteral" ` + `functions are also provided.` if err == nil || err.Error() != expectedError { t.Fatalf("Expected error: %v, got %v", expectedError, err) } }
func TestTypeSystem_ScalarTypesMustBeSerializable_AcceptsAScalarTypeDefiningSerialize(t *testing.T) { _, err := schemaWithFieldType(graphql.NewScalar(graphql.ScalarConfig{ Name: "SomeScalar", Serialize: func(value interface{}) interface{} { return nil }, })) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func TestTypeSystem_ScalarTypesMustBeSerializable_RejectsAScalarTypeDefiningParseLiteralButNotParseValue(t *testing.T) { _, err := schemaWithFieldType(graphql.NewScalar(graphql.ScalarConfig{ Name: "SomeScalar", Serialize: func(value interface{}) interface{} { return nil }, ParseLiteral: func(valueAST ast.Value) interface{} { return nil }, })) expectedError := `SomeScalar must provide both "parseValue" and "parseLiteral" functions.` if err == nil || err.Error() != expectedError { t.Fatalf("Expected error: %v, got %v", expectedError, err) } }
package graphql_test import ( "testing" "github.com/graphql-go/graphql" "github.com/graphql-go/graphql/language/ast" ) var someScalarType = graphql.NewScalar(graphql.ScalarConfig{ Name: "SomeScalar", Serialize: func(value interface{}) interface{} { return nil }, ParseValue: func(value interface{}) interface{} { return nil }, ParseLiteral: func(valueAST ast.Value) interface{} { return nil }, }) var someObjectType = graphql.NewObject(graphql.ObjectConfig{ Name: "SomeObject", Fields: graphql.FieldConfigMap{ "f": &graphql.FieldConfig{ Type: graphql.String, }, }, }) var objectWithIsTypeOf = graphql.NewObject(graphql.ObjectConfig{ Name: "ObjectWithIsTypeOf",
"github.com/graphql-go/graphql/language/ast" "github.com/graphql-go/graphql/language/location" "github.com/graphql-go/graphql/testutil" ) var testComplexScalar *graphql.Scalar = graphql.NewScalar(graphql.ScalarConfig{ Name: "ComplexScalar", Serialize: func(value interface{}) interface{} { if value == "DeserializedValue" { return "SerializedValue" } return nil }, ParseValue: func(value interface{}) interface{} { if value == "SerializedValue" { return "DeserializedValue" } return nil }, ParseLiteral: func(valueAST ast.Value) interface{} { astValue := valueAST.GetValue() 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{