Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
647638d1
Commit
647638d1
authored
Jul 30, 2020
by
Luke Duncalfe
Committed by
Evan Read
Jul 30, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
GraphQL docs: Handling data stored as JSON
parent
d0b35fde
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
0 deletions
+46
-0
doc/development/api_graphql_styleguide.md
doc/development/api_graphql_styleguide.md
+46
-0
No files found.
doc/development/api_graphql_styleguide.md
View file @
647638d1
...
...
@@ -429,6 +429,52 @@ module Types
end
```
## JSON
When data to be returned by GraphQL is stored as
[
JSON
](
migration_style_guide.md#storing-json-in-database
)
, we should continue to use
GraphQL types whenever possible. Avoid using the
`GraphQL::Types::JSON`
type unless
the JSON data returned is _truly_ unstructured.
If the structure of the JSON data varies, but will be one of a set of known possible
structures, use a
[
union
](
https://graphql-ruby.org/type_definitions/unions.html
)
.
An example of the use of a union for this purpose is
[
!30129
](
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/30129
)
.
Field names can be mapped to hash data keys using the
`hash_key:`
keyword if needed.
For example, given the following simple JSON data:
```
json
{
"title"
:
"My chart"
,
"data"
:
[
{
"x"
:
0
,
"y"
:
1
},
{
"x"
:
1
,
"y"
:
1
},
{
"x"
:
2
,
"y"
:
2
}
]
}
```
We can use GraphQL types like this:
```
ruby
module
Types
class
ChartType
<
BaseObject
field
:title
,
GraphQL
::
STRING_TYPE
,
null:
true
,
description:
'Title of the chart'
field
:data
,
[
Types
::
ChartDatumType
],
null:
true
,
description:
'Data of the chart'
end
end
module
Types
class
ChartDatumType
<
BaseObject
field
:x
,
GraphQL
::
INT_TYPE
,
null:
true
,
description:
'X-axis value of the chart datum'
field
:y
,
GraphQL
::
INT_TYPE
,
null:
true
,
description:
'Y-axis value of the chart datum'
end
end
```
## Descriptions
All fields and arguments
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment