You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,4 +7,4 @@
7
7
This repository contains the source code for the GraphQL ASP.NET documentation website. Clone the `develop` branch to view the code and update the documentation. The `master` branch contains the compiled static site served by github pages.
8
8
9
9
10
-
_Documentation created using [Docusaurus v1.14.0](https://docusaurus.io)_
10
+
_Documentation created using [Docusaurus v2](https://docusaurus.io)_
Copy file name to clipboardExpand all lines: docs/advanced/custom-scalars.md
+35-51Lines changed: 35 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
id: custom-scalars
3
3
title: Custom Scalars
4
4
sidebar_label: Custom Scalars
5
+
sidebar_position: 3
5
6
---
6
7
7
8
Scalars are the most basic, fundamental unit of content in GraphQL. It is one of two leaf types (the other being [ENUMS](../types/enums)). When a query is resolved the returned data will be a set of nested key/value pairs where every key is a string and every value is either another set of key/value pairs, an enum or a scalar.
@@ -12,11 +13,7 @@ This can be done for any value that can be represented as a simple set of charac
12
13
13
14
Lets say we wanted to build a scalar called `Money` that can handle both an amount and currency symbol. We might accept it in a query like this:
14
15
15
-
<divclass="sideBySideCode hljs">
16
-
<div>
17
-
18
16
```csharp
19
-
// C# Controller
20
17
publicclassInventoryController : GraphController
21
18
{
22
19
[QueryRoot("search")]
@@ -42,10 +39,7 @@ public class Money
42
39
}
43
40
```
44
41
45
-
</div>
46
-
<div>
47
-
48
-
```javascript
42
+
```graphql title="Using the Money Scalar"
49
43
query {
50
44
search(minPrice: "$18.45"){
51
45
id
@@ -54,44 +48,38 @@ query {
54
48
}
55
49
```
56
50
57
-
</div>
58
-
</div>
59
-
<br/>
60
51
61
52
The query supplies the data as a quoted string, `"$18.45"`, but our action method receives a `Money` object. Internally, GraphQL senses that the value should be `Money` from the schema definition and invokes the correct resolver to parse the value and generate the .NET object that can be passed to our action method.
62
53
63
54
## Implement IScalarGraphType
64
55
65
56
To create a scalar graph type we need to implement `IScalarGraphType` and register it with GraphQL. The methods and properties of `IScalarGraphType` are as follows:
66
57
67
-
```csharp
68
-
namespaceGraphQL.AspNet.Interfaces.TypeSystem
58
+
```csharp title="IScalarGraphType.cs"
59
+
publicinterfaceIScalarGraphType
69
60
{
70
-
publicinterfaceIScalarGraphType
71
-
{
72
-
stringName { get; }
73
-
stringInternalName { get; }
74
-
stringDescription { get; }
75
-
TypeKindKind { get; }
76
-
boolPublish { get; }
77
-
ScalarValueTypeValueType { get; }
78
-
TypeObjectType { get; }
79
-
TypeCollectionOtherKnownTypes { get; }
80
-
ILeafValueResolverSourceResolver { get; }
81
-
IScalarValueSerializerSerializer { get; }
82
-
83
-
boolValidateObject(objectitem);
84
-
}
61
+
stringName { get; }
62
+
stringInternalName { get; }
63
+
stringDescription { get; }
64
+
TypeKindKind { get; }
65
+
boolPublish { get; }
66
+
ScalarValueTypeValueType { get; }
67
+
TypeObjectType { get; }
68
+
TypeCollectionOtherKnownTypes { get; }
69
+
ILeafValueResolverSourceResolver { get; }
70
+
IScalarValueSerializerSerializer { get; }
71
+
72
+
boolValidateObject(objectitem);
73
+
}
85
74
86
-
publicinterfaceILeafValueResolver
87
-
{
88
-
objectResolve(ReadOnlySpan<char> data);
89
-
}
75
+
publicinterfaceILeafValueResolver
76
+
{
77
+
objectResolve(ReadOnlySpan<char> data);
78
+
}
90
79
91
-
publicinterfaceIScalarValueSerializer
92
-
{
93
-
objectSerialize(objectitem);
94
-
}
80
+
publicinterfaceIScalarValueSerializer
81
+
{
82
+
objectSerialize(objectitem);
95
83
}
96
84
```
97
85
@@ -148,7 +136,7 @@ If you throw `UnresolvedValueException` your error message will be delivered ver
148
136
149
137
Taking a look at the at the serializer for the `Guid` scalar type we can see that while internally the `System.Guid` struct represents the value we convert it to a string when serializing it. Most scalar implementations will serialize to a string.
@@ -231,27 +219,25 @@ The completed Money custom scalar type
231
219
232
220
The last step in declaring a scalar is to register it with the runtime. Scalars are schema agnostic. They sit outside of any dependency injection context and must be registered directly with GraphQL.
Since our scalar is represented by a .NET class, if we don't pre-register it GraphQL will attempt to parse the `Money` class as an object graph type. Once registered as a scalar, any attempt to use `Money` as an object graph type will cause an exception.
232
+
:::
247
233
248
234
## @specifiedBy Directive
249
235
250
236
GraphQL provides a special, built-in directive called `@specifiedBy` that allows you to supply a URL pointing to a the specification for your custom scalar. This url is used by various tools to additional data to your customers so they know how to interact with your scalar type. It is entirely optional.
251
237
252
238
The @specifiedBy directive can be applied to a scalar in all the same ways as other type system directives or by use of the special `[SpecifiedBy]` attribute.
0 commit comments