Skip to content

Commit

Permalink
Return new instances of custom scalar gql types
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jun 25, 2024
1 parent a42a05c commit d6bb1ef
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
4 changes: 2 additions & 2 deletions internal/request/graphql/schema/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var (
client.FieldKind_NILLABLE_STRING: gql.String,
client.FieldKind_STRING_ARRAY: gql.NewList(gql.NewNonNull(gql.String)),
client.FieldKind_NILLABLE_STRING_ARRAY: gql.NewList(gql.String),
client.FieldKind_NILLABLE_BLOB: schemaTypes.BlobScalarType,
client.FieldKind_NILLABLE_JSON: schemaTypes.JSONScalarType,
client.FieldKind_NILLABLE_BLOB: schemaTypes.BlobScalarType(),
client.FieldKind_NILLABLE_JSON: schemaTypes.JSONScalarType(),
}

defaultCRDTForFieldKind = map[client.FieldKind]client.CType{
Expand Down
4 changes: 2 additions & 2 deletions internal/request/graphql/schema/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func defaultTypes(
gql.String,

// Custom Scalar types
schemaTypes.BlobScalarType,
schemaTypes.JSONScalarType,
schemaTypes.BlobScalarType(),
schemaTypes.JSONScalarType(),

// Base Query types

Expand Down
76 changes: 40 additions & 36 deletions internal/request/graphql/schema/types/scalars.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,26 @@ func coerceBlob(value any) any {
}
}

var BlobScalarType = graphql.NewScalar(graphql.ScalarConfig{
Name: "Blob",
Description: "The `Blob` scalar type represents a binary large object.",
// Serialize converts the value to a hex string
Serialize: coerceBlob,
// ParseValue converts the value to a hex string
ParseValue: coerceBlob,
// ParseLiteral converts the ast value to a hex string
ParseLiteral: func(valueAST ast.Value) any {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return coerceBlob(valueAST.Value)
default:
// return nil if the value cannot be parsed
return nil
}
},
})
func BlobScalarType() *graphql.Scalar {
return graphql.NewScalar(graphql.ScalarConfig{
Name: "Blob",
Description: "The `Blob` scalar type represents a binary large object.",
// Serialize converts the value to a hex string
Serialize: coerceBlob,
// ParseValue converts the value to a hex string
ParseValue: coerceBlob,
// ParseLiteral converts the ast value to a hex string
ParseLiteral: func(valueAST ast.Value) any {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return coerceBlob(valueAST.Value)
default:
// return nil if the value cannot be parsed
return nil
}
},
})
}

// coerceJSON converts the given value into a valid json string.
// If the value cannot be converted nil is returned.
Expand Down Expand Up @@ -98,21 +100,23 @@ func coerceJSON(value any) any {
}
}

var JSONScalarType = graphql.NewScalar(graphql.ScalarConfig{
Name: "JSON",
Description: "The `JSON` scalar type represents a JSON string.",
// Serialize converts the value to a json string
Serialize: coerceJSON,
// ParseValue converts the value to a json string
ParseValue: coerceJSON,
// ParseLiteral converts the ast value to a json string
ParseLiteral: func(valueAST ast.Value) any {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return coerceJSON(valueAST.Value)
default:
// return nil if the value cannot be parsed
return nil
}
},
})
func JSONScalarType() *graphql.Scalar {
return graphql.NewScalar(graphql.ScalarConfig{
Name: "JSON",
Description: "The `JSON` scalar type represents a JSON string.",
// Serialize converts the value to a json string
Serialize: coerceJSON,
// ParseValue converts the value to a json string
ParseValue: coerceJSON,
// ParseLiteral converts the ast value to a json string
ParseLiteral: func(valueAST ast.Value) any {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return coerceJSON(valueAST.Value)
default:
// return nil if the value cannot be parsed
return nil
}
},
})
}
12 changes: 6 additions & 6 deletions internal/request/graphql/schema/types/scalars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBlobScalarTypeSerialize(t *testing.T) {
{false, nil},
}
for _, c := range cases {
result := BlobScalarType.Serialize(c.input)
result := BlobScalarType().Serialize(c.input)
assert.Equal(t, c.expect, result)
}
}
Expand All @@ -60,7 +60,7 @@ func TestBlobScalarTypeParseValue(t *testing.T) {
{false, nil},
}
for _, c := range cases {
result := BlobScalarType.ParseValue(c.input)
result := BlobScalarType().ParseValue(c.input)
assert.Equal(t, c.expect, result)
}
}
Expand All @@ -82,7 +82,7 @@ func TestBlobScalarTypeParseLiteral(t *testing.T) {
{&ast.ObjectValue{}, nil},
}
for _, c := range cases {
result := BlobScalarType.ParseLiteral(c.input)
result := BlobScalarType().ParseLiteral(c.input)
assert.Equal(t, c.expect, result)
}
}
Expand Down Expand Up @@ -141,10 +141,10 @@ func TestJSONScalarTypeParseAndSerialize(t *testing.T) {
{false, nil},
}
for _, c := range cases {
parsed := JSONScalarType.ParseValue(c.input)
parsed := JSONScalarType().ParseValue(c.input)
assert.Equal(t, c.expect, parsed)

serialized := JSONScalarType.Serialize(c.input)
serialized := JSONScalarType().Serialize(c.input)
assert.Equal(t, c.expect, serialized)
}
}
Expand All @@ -165,7 +165,7 @@ func TestJSONScalarTypeParseLiteral(t *testing.T) {
{&ast.ObjectValue{}, nil},
}
for _, c := range cases {
result := JSONScalarType.ParseLiteral(c.input)
result := JSONScalarType().ParseLiteral(c.input)
assert.Equal(t, c.expect, result)
}
}

0 comments on commit d6bb1ef

Please sign in to comment.