Skip to content

Commit

Permalink
Get rid of global gql scalar variables
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Jul 17, 2024
1 parent b33a376 commit 2e61ab0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 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
15 changes: 9 additions & 6 deletions internal/request/graphql/schema/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ func defaultTypes(
crdtEnum *gql.Enum,
explainEnum *gql.Enum,
) []gql.Type {
blobScalarType := schemaTypes.BlobScalarType()
jsonScalarType := schemaTypes.JSONScalarType()

return []gql.Type{
// Base Scalar types
gql.Boolean,
Expand All @@ -176,8 +179,8 @@ func defaultTypes(
gql.String,

// Custom Scalar types
schemaTypes.BlobScalarType,
schemaTypes.JSONScalarType,
blobScalarType,
jsonScalarType,

// Base Query types

Expand All @@ -195,10 +198,10 @@ func defaultTypes(
schemaTypes.NotNullIntOperatorBlock(),
schemaTypes.StringOperatorBlock(),
schemaTypes.NotNullstringOperatorBlock(),
schemaTypes.JSONOperatorBlock(),
schemaTypes.NotNullJSONOperatorBlock(),
schemaTypes.BlobOperatorBlock(),
schemaTypes.NotNullBlobOperatorBlock(),
schemaTypes.JSONOperatorBlock(jsonScalarType),
schemaTypes.NotNullJSONOperatorBlock(jsonScalarType),
schemaTypes.BlobOperatorBlock(blobScalarType),
schemaTypes.NotNullBlobOperatorBlock(blobScalarType),

commitsOrderArg,
commitLinkObject,
Expand Down
40 changes: 20 additions & 20 deletions internal/request/graphql/schema/types/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,26 @@ func NotNullstringOperatorBlock() *gql.InputObject {
}

// JSONOperatorBlock filter block for string types.
func JSONOperatorBlock() *gql.InputObject {
func JSONOperatorBlock(jsonScalarType *gql.Scalar) *gql.InputObject {
return gql.NewInputObject(gql.InputObjectConfig{
Name: "JSONOperatorBlock",
Description: stringOperatorBlockDescription,
Fields: gql.InputObjectConfigFieldMap{
"_eq": &gql.InputObjectFieldConfig{
Description: eqOperatorDescription,
Type: JSONScalarType,
Type: jsonScalarType,
},
"_ne": &gql.InputObjectFieldConfig{
Description: neOperatorDescription,
Type: JSONScalarType,
Type: jsonScalarType,
},
"_in": &gql.InputObjectFieldConfig{
Description: inOperatorDescription,
Type: gql.NewList(JSONScalarType),
Type: gql.NewList(jsonScalarType),
},
"_nin": &gql.InputObjectFieldConfig{
Description: ninOperatorDescription,
Type: gql.NewList(JSONScalarType),
Type: gql.NewList(jsonScalarType),
},
"_like": &gql.InputObjectFieldConfig{
Description: likeStringOperatorDescription,
Expand All @@ -403,26 +403,26 @@ func JSONOperatorBlock() *gql.InputObject {
}

// NotNullJSONOperatorBlock filter block for string! types.
func NotNullJSONOperatorBlock() *gql.InputObject {
func NotNullJSONOperatorBlock(jsonScalarType *gql.Scalar) *gql.InputObject {
return gql.NewInputObject(gql.InputObjectConfig{
Name: "NotNullJSONOperatorBlock",
Description: notNullStringOperatorBlockDescription,
Fields: gql.InputObjectConfigFieldMap{
"_eq": &gql.InputObjectFieldConfig{
Description: eqOperatorDescription,
Type: JSONScalarType,
Type: jsonScalarType,
},
"_ne": &gql.InputObjectFieldConfig{
Description: neOperatorDescription,
Type: JSONScalarType,
Type: jsonScalarType,
},
"_in": &gql.InputObjectFieldConfig{
Description: inOperatorDescription,
Type: gql.NewList(gql.NewNonNull(JSONScalarType)),
Type: gql.NewList(gql.NewNonNull(jsonScalarType)),
},
"_nin": &gql.InputObjectFieldConfig{
Description: ninOperatorDescription,
Type: gql.NewList(gql.NewNonNull(JSONScalarType)),
Type: gql.NewList(gql.NewNonNull(jsonScalarType)),
},
"_like": &gql.InputObjectFieldConfig{
Description: likeStringOperatorDescription,
Expand All @@ -444,26 +444,26 @@ func NotNullJSONOperatorBlock() *gql.InputObject {
})
}

func BlobOperatorBlock() *gql.InputObject {
func BlobOperatorBlock(blobScalarType *gql.Scalar) *gql.InputObject {
return gql.NewInputObject(gql.InputObjectConfig{
Name: "BlobOperatorBlock",
Description: stringOperatorBlockDescription,
Fields: gql.InputObjectConfigFieldMap{
"_eq": &gql.InputObjectFieldConfig{
Description: eqOperatorDescription,
Type: BlobScalarType,
Type: blobScalarType,
},
"_ne": &gql.InputObjectFieldConfig{
Description: neOperatorDescription,
Type: BlobScalarType,
Type: blobScalarType,
},
"_in": &gql.InputObjectFieldConfig{
Description: inOperatorDescription,
Type: gql.NewList(BlobScalarType),
Type: gql.NewList(blobScalarType),
},
"_nin": &gql.InputObjectFieldConfig{
Description: ninOperatorDescription,
Type: gql.NewList(BlobScalarType),
Type: gql.NewList(blobScalarType),
},
"_like": &gql.InputObjectFieldConfig{
Description: likeStringOperatorDescription,
Expand All @@ -486,26 +486,26 @@ func BlobOperatorBlock() *gql.InputObject {
}

// NotNullJSONOperatorBlock filter block for string! types.
func NotNullBlobOperatorBlock() *gql.InputObject {
func NotNullBlobOperatorBlock(blobScalarType *gql.Scalar) *gql.InputObject {
return gql.NewInputObject(gql.InputObjectConfig{
Name: "NotNullBlobOperatorBlock",
Description: notNullStringOperatorBlockDescription,
Fields: gql.InputObjectConfigFieldMap{
"_eq": &gql.InputObjectFieldConfig{
Description: eqOperatorDescription,
Type: BlobScalarType,
Type: blobScalarType,
},
"_ne": &gql.InputObjectFieldConfig{
Description: neOperatorDescription,
Type: BlobScalarType,
Type: blobScalarType,
},
"_in": &gql.InputObjectFieldConfig{
Description: inOperatorDescription,
Type: gql.NewList(gql.NewNonNull(BlobScalarType)),
Type: gql.NewList(gql.NewNonNull(blobScalarType)),
},
"_nin": &gql.InputObjectFieldConfig{
Description: ninOperatorDescription,
Type: gql.NewList(gql.NewNonNull(BlobScalarType)),
Type: gql.NewList(gql.NewNonNull(blobScalarType)),
},
"_like": &gql.InputObjectFieldConfig{
Description: likeStringOperatorDescription,
Expand Down
8 changes: 2 additions & 6 deletions internal/request/graphql/schema/types/scalars.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func coerceBlob(value any) any {
}
}

func NewBlobScalarType() *graphql.Scalar {
func BlobScalarType() *graphql.Scalar {
return graphql.NewScalar(graphql.ScalarConfig{
Name: "Blob",
Description: "The `Blob` scalar type represents a binary large object.",
Expand All @@ -67,8 +67,6 @@ func NewBlobScalarType() *graphql.Scalar {
})
}

var BlobScalarType = NewBlobScalarType()

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

func NewJSONScalarType() *graphql.Scalar {
func JSONScalarType() *graphql.Scalar {
return graphql.NewScalar(graphql.ScalarConfig{
Name: "JSON",
Description: "The `JSON` scalar type represents a JSON string.",
Expand All @@ -122,5 +120,3 @@ func NewJSONScalarType() *graphql.Scalar {
},
})
}

var JSONScalarType = NewJSONScalarType()
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 := NewBlobScalarType().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 := NewBlobScalarType().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 := NewBlobScalarType().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 2e61ab0

Please sign in to comment.