Skip to content

Commit 5ff368f

Browse files
authored
Merge pull request #146 from graphql-java/scalars-update
Update deprecated methods in scalar coercion documentation
2 parents f452b7c + e003761 commit 5ff368f

3 files changed

Lines changed: 48 additions & 42 deletions

File tree

documentation/scalars.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Scalars"
3-
date: 2018-09-09T12:52:46+10:00
3+
date: 2024-01-11
44
description: How scalar types work in graphql and how to write your own scalars
55
---
66
# Scalars in GraphQL
@@ -21,20 +21,22 @@ The [GraphQL specification](https://spec.graphql.org/draft/#sec-Scalars) states
2121

2222
The class ``graphql.Scalars`` contains singleton instances of the provided scalar types.
2323

24-
[graphql-java-extended-scalars](https://github.com/graphql-java/graphql-java-extended-scalars) adds the following scalar types which are useful in Java based systems:
24+
[graphql-java-extended-scalars](https://github.com/graphql-java/graphql-java-extended-scalars) adds many more scalars, including the following which are useful in Java based systems:
2525

2626
* Long aka ``GraphQLLong`` - a java.lang.Long based scalar
2727
* Short aka ``GraphQLShort`` - a java.lang.Short based scalar
2828
* Byte aka ``GraphQLByte`` - a java.lang.Byte based scalar
2929
* BigDecimal aka ``GraphQLBigDecimal`` - a java.math.BigDecimal based scalar
3030
* BigInteger aka ``GraphQLBigInteger`` - a java.math.BigInteger based scalar
3131

32+
See the [documentation](https://github.com/graphql-java/graphql-java-extended-scalars) for how to use Extended Scalars.
33+
3234
## Writing your own Custom Scalars
3335

34-
You can write your own custom scalar implementations. In doing so you take on the responsibility for coercing values
36+
If the scalar you want isn't in a library, you can also write your own custom scalar implementation. In doing so you take on the responsibility for coercing values
3537
at runtime, which we will explain in a moment.
3638

37-
Imagine we decide we need to have an email scalar type. It will take email addresses as input and output.
39+
Imagine we decide we need to have an email scalar type. It will take email addresses as input and output.
3840

3941
We would create a singleton ``graphql.schema.GraphQLScalarType`` instance for this.
4042

@@ -44,17 +46,17 @@ public static final GraphQLScalarType EMAIL = GraphQLScalarType.newScalar()
4446
.description("A custom scalar that handles emails")
4547
.coercing(new Coercing() {
4648
@Override
47-
public Object serialize(Object dataFetcherResult) {
49+
public Object serialize(Object dataFetcherResult, GraphQLContext graphQLContext, Locale locale) {
4850
return serializeEmail(dataFetcherResult);
4951
}
5052

5153
@Override
52-
public Object parseValue(Object input) {
54+
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) {
5355
return parseEmailFromVariable(input);
5456
}
5557

5658
@Override
57-
public Object parseLiteral(Object input) {
59+
public Object parseLiteral(Value input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
5860
return parseEmailFromAstLiteral(input);
5961
}
6062
})
@@ -122,17 +124,17 @@ public static class EmailScalar {
122124
.description("A custom scalar that handles emails")
123125
.coercing(new Coercing() {
124126
@Override
125-
public Object serialize(Object dataFetcherResult) {
127+
public Object serialize(Object dataFetcherResult, GraphQLContext graphQLContext, Locale locale) {
126128
return serializeEmail(dataFetcherResult);
127129
}
128130

129131
@Override
130-
public Object parseValue(Object input) {
132+
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) {
131133
return parseEmailFromVariable(input);
132134
}
133135

134136
@Override
135-
public Object parseLiteral(Object input) {
137+
public Object parseLiteral(Value input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
136138
return parseEmailFromAstLiteral(input);
137139
}
138140
})

versioned_docs/version-v20/scalars.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Scalars"
3-
date: 2018-09-09T12:52:46+10:00
3+
date: 2024-01-11
44
description: How scalar types work in graphql and how to write your own scalars
55
---
66
# Scalars in GraphQL
@@ -21,20 +21,22 @@ The [GraphQL specification](https://spec.graphql.org/draft/#sec-Scalars) states
2121

2222
The class ``graphql.Scalars`` contains singleton instances of the provided scalar types.
2323

24-
[graphql-java-extended-scalars](https://github.com/graphql-java/graphql-java-extended-scalars) adds the following scalar types which are useful in Java based systems:
24+
[graphql-java-extended-scalars](https://github.com/graphql-java/graphql-java-extended-scalars) adds many more scalars, including the following which are useful in Java based systems:
2525

2626
* Long aka ``GraphQLLong`` - a java.lang.Long based scalar
2727
* Short aka ``GraphQLShort`` - a java.lang.Short based scalar
2828
* Byte aka ``GraphQLByte`` - a java.lang.Byte based scalar
2929
* BigDecimal aka ``GraphQLBigDecimal`` - a java.math.BigDecimal based scalar
3030
* BigInteger aka ``GraphQLBigInteger`` - a java.math.BigInteger based scalar
3131

32+
See the [documentation](https://github.com/graphql-java/graphql-java-extended-scalars) for how to use Extended Scalars.
33+
3234
## Writing your own Custom Scalars
3335

34-
You can write your own custom scalar implementations. In doing so you take on the responsibility for coercing values
36+
If the scalar you want isn't in a library, you can also write your own custom scalar implementation. In doing so you take on the responsibility for coercing values
3537
at runtime, which we will explain in a moment.
3638

37-
Imagine we decide we need to have an email scalar type. It will take email addresses as input and output.
39+
Imagine we decide we need to have an email scalar type. It will take email addresses as input and output.
3840

3941
We would create a singleton ``graphql.schema.GraphQLScalarType`` instance for this.
4042

@@ -44,17 +46,17 @@ public static final GraphQLScalarType EMAIL = GraphQLScalarType.newScalar()
4446
.description("A custom scalar that handles emails")
4547
.coercing(new Coercing() {
4648
@Override
47-
public Object serialize(Object dataFetcherResult) {
49+
public Object serialize(Object dataFetcherResult, GraphQLContext graphQLContext, Locale locale) {
4850
return serializeEmail(dataFetcherResult);
4951
}
5052

5153
@Override
52-
public Object parseValue(Object input) {
54+
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) {
5355
return parseEmailFromVariable(input);
5456
}
5557

5658
@Override
57-
public Object parseLiteral(Object input) {
59+
public Object parseLiteral(Value input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
5860
return parseEmailFromAstLiteral(input);
5961
}
6062
})
@@ -96,16 +98,16 @@ and output are indeed email addresses.
9698
The JavaDoc method contract of ``graphql.schema.Coercing`` says the following:
9799

98100
* The ``serialize`` MUST ONLY allow ``graphql.schema.CoercingSerializeException`` to be thrown from it. This indicates that the
99-
value cannot be serialized into an appropriate form. You must not allow other runtime exceptions to escape this method to get
100-
the normal graphql behaviour for validation.
101+
value cannot be serialized into an appropriate form. You must not allow other runtime exceptions to escape this method to get
102+
the normal graphql behaviour for validation.
101103

102104
* The ``parseValue`` MUST ONLY allow ``graphql.schema.CoercingParseValueException`` to be thrown from it. This indicates that the
103-
value cannot be parsed as input into an appropriate form. You must not allow other runtime exceptions to escape this method to get
104-
the normal graphql behaviour for validation.
105+
value cannot be parsed as input into an appropriate form. You must not allow other runtime exceptions to escape this method to get
106+
the normal graphql behaviour for validation.
105107

106108
* The ``parseLiteral`` MUST ONLY allow ``graphql.schema.CoercingParseLiteralException`` to be thrown from it. This indicates that the
107-
AST value cannot be parsed as input into an appropriate form. You must not allow any runtime exceptions to escape this method to get
108-
the normal graphql behaviour for validation.
109+
AST value cannot be parsed as input into an appropriate form. You must not allow any runtime exceptions to escape this method to get
110+
the normal graphql behaviour for validation.
109111

110112
Some people try to rely on runtime exceptions for validation and hope that they come out as graphql errors. This is not the case. You
111113
MUST follow the ``Coercing`` method contracts to allow the graphql-java engine to work according to the graphql specification on scalar types.
@@ -122,17 +124,17 @@ public static class EmailScalar {
122124
.description("A custom scalar that handles emails")
123125
.coercing(new Coercing() {
124126
@Override
125-
public Object serialize(Object dataFetcherResult) {
127+
public Object serialize(Object dataFetcherResult, GraphQLContext graphQLContext, Locale locale) {
126128
return serializeEmail(dataFetcherResult);
127129
}
128130

129131
@Override
130-
public Object parseValue(Object input) {
132+
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) {
131133
return parseEmailFromVariable(input);
132134
}
133135

134136
@Override
135-
public Object parseLiteral(Object input) {
137+
public Object parseLiteral(Value input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
136138
return parseEmailFromAstLiteral(input);
137139
}
138140
})

versioned_docs/version-v21/scalars.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Scalars"
3-
date: 2018-09-09T12:52:46+10:00
3+
date: 2024-01-11
44
description: How scalar types work in graphql and how to write your own scalars
55
---
66
# Scalars in GraphQL
@@ -21,20 +21,22 @@ The [GraphQL specification](https://spec.graphql.org/draft/#sec-Scalars) states
2121

2222
The class ``graphql.Scalars`` contains singleton instances of the provided scalar types.
2323

24-
[graphql-java-extended-scalars](https://github.com/graphql-java/graphql-java-extended-scalars) adds the following scalar types which are useful in Java based systems:
24+
[graphql-java-extended-scalars](https://github.com/graphql-java/graphql-java-extended-scalars) adds many more scalars, including the following which are useful in Java based systems:
2525

2626
* Long aka ``GraphQLLong`` - a java.lang.Long based scalar
2727
* Short aka ``GraphQLShort`` - a java.lang.Short based scalar
2828
* Byte aka ``GraphQLByte`` - a java.lang.Byte based scalar
2929
* BigDecimal aka ``GraphQLBigDecimal`` - a java.math.BigDecimal based scalar
3030
* BigInteger aka ``GraphQLBigInteger`` - a java.math.BigInteger based scalar
3131

32+
See the [documentation](https://github.com/graphql-java/graphql-java-extended-scalars) for how to use Extended Scalars.
33+
3234
## Writing your own Custom Scalars
3335

34-
You can write your own custom scalar implementations. In doing so you take on the responsibility for coercing values
36+
If the scalar you want isn't in a library, you can also write your own custom scalar implementation. In doing so you take on the responsibility for coercing values
3537
at runtime, which we will explain in a moment.
3638

37-
Imagine we decide we need to have an email scalar type. It will take email addresses as input and output.
39+
Imagine we decide we need to have an email scalar type. It will take email addresses as input and output.
3840

3941
We would create a singleton ``graphql.schema.GraphQLScalarType`` instance for this.
4042

@@ -44,17 +46,17 @@ public static final GraphQLScalarType EMAIL = GraphQLScalarType.newScalar()
4446
.description("A custom scalar that handles emails")
4547
.coercing(new Coercing() {
4648
@Override
47-
public Object serialize(Object dataFetcherResult) {
49+
public Object serialize(Object dataFetcherResult, GraphQLContext graphQLContext, Locale locale) {
4850
return serializeEmail(dataFetcherResult);
4951
}
5052

5153
@Override
52-
public Object parseValue(Object input) {
54+
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) {
5355
return parseEmailFromVariable(input);
5456
}
5557

5658
@Override
57-
public Object parseLiteral(Object input) {
59+
public Object parseLiteral(Value input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
5860
return parseEmailFromAstLiteral(input);
5961
}
6062
})
@@ -96,16 +98,16 @@ and output are indeed email addresses.
9698
The JavaDoc method contract of ``graphql.schema.Coercing`` says the following:
9799

98100
* The ``serialize`` MUST ONLY allow ``graphql.schema.CoercingSerializeException`` to be thrown from it. This indicates that the
99-
value cannot be serialized into an appropriate form. You must not allow other runtime exceptions to escape this method to get
100-
the normal graphql behaviour for validation.
101+
value cannot be serialized into an appropriate form. You must not allow other runtime exceptions to escape this method to get
102+
the normal graphql behaviour for validation.
101103

102104
* The ``parseValue`` MUST ONLY allow ``graphql.schema.CoercingParseValueException`` to be thrown from it. This indicates that the
103-
value cannot be parsed as input into an appropriate form. You must not allow other runtime exceptions to escape this method to get
104-
the normal graphql behaviour for validation.
105+
value cannot be parsed as input into an appropriate form. You must not allow other runtime exceptions to escape this method to get
106+
the normal graphql behaviour for validation.
105107

106108
* The ``parseLiteral`` MUST ONLY allow ``graphql.schema.CoercingParseLiteralException`` to be thrown from it. This indicates that the
107-
AST value cannot be parsed as input into an appropriate form. You must not allow any runtime exceptions to escape this method to get
108-
the normal graphql behaviour for validation.
109+
AST value cannot be parsed as input into an appropriate form. You must not allow any runtime exceptions to escape this method to get
110+
the normal graphql behaviour for validation.
109111

110112
Some people try to rely on runtime exceptions for validation and hope that they come out as graphql errors. This is not the case. You
111113
MUST follow the ``Coercing`` method contracts to allow the graphql-java engine to work according to the graphql specification on scalar types.
@@ -122,17 +124,17 @@ public static class EmailScalar {
122124
.description("A custom scalar that handles emails")
123125
.coercing(new Coercing() {
124126
@Override
125-
public Object serialize(Object dataFetcherResult) {
127+
public Object serialize(Object dataFetcherResult, GraphQLContext graphQLContext, Locale locale) {
126128
return serializeEmail(dataFetcherResult);
127129
}
128130

129131
@Override
130-
public Object parseValue(Object input) {
132+
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) {
131133
return parseEmailFromVariable(input);
132134
}
133135

134136
@Override
135-
public Object parseLiteral(Object input) {
137+
public Object parseLiteral(Value input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
136138
return parseEmailFromAstLiteral(input);
137139
}
138140
})

0 commit comments

Comments
 (0)