-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from evo-company/add-unions-support
add unions support
- Loading branch information
Showing
42 changed files
with
2,114 additions
and
739 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ User's Guide | |
asyncio | ||
graphql | ||
protobuf | ||
unions | ||
directives | ||
federation | ||
telemetry | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
Unions | ||
====== | ||
|
||
.. _unions-doc: | ||
|
||
Union types are special types used to represent a value that could be one of a number of types. | ||
|
||
In graphql you can use union types like this: | ||
|
||
.. code-block:: | ||
type Audio { | ||
id: ID! | ||
duration: Int! | ||
} | ||
type Video { | ||
id: ID! | ||
thumbnailUrl: String! | ||
} | ||
union Media = Audio | Video | ||
type Query { | ||
search(text: String!): [Media!]! | ||
} | ||
In `hiku` you can use union types like this: | ||
|
||
.. code-block:: python | ||
from hiku.graph import Field, Graph, Link, Node, Root, Union | ||
from hiku.types import ID, Integer, String, TypeRef, Sequence, Optional, UnionRef | ||
def search_resolver(): | ||
return [ | ||
(1, TypeRef['Audio']), | ||
(2, TypeRef['Video']), | ||
] | ||
unions = [ | ||
Union('Media', ['Audio', 'Video']), | ||
] | ||
GRAPH = Graph([ | ||
Node('Audio', [ | ||
Field('id', ID, audio_fields_resolver), | ||
Field('duration', Integer, audio_fields_resolver), | ||
]), | ||
Node('Video', [ | ||
Field('id', ID, video_fields_resolver), | ||
Field('thumbnailUrl', String, video_fields_resolver), | ||
]), | ||
Root([ | ||
Link('search', Sequence(UnionRef['Media']), search_resolver, requires=None), | ||
]), | ||
], unions=unions) | ||
Lets look at the example above: | ||
|
||
- `Union` type is defined with a list of types that are part of the union - `Union('Media', ['Audio', 'Video'])` | ||
- `Link` type is defined with a return type of `Sequence[UnionRef['Media']]` | ||
- `search_resolver` returns a list of tuples with an id as a first tuple element and type as a second tuple element | ||
|
||
.. note:: | ||
|
||
`UnionRef` is a special type that is used to reference union types. It is used in the example above to define | ||
the return type of the `search` link. | ||
|
||
Now lets look at the query: | ||
|
||
.. code-block:: python | ||
query { | ||
search(text: "test") { | ||
__typename | ||
id | ||
... on Audio { | ||
duration | ||
} | ||
... on Video { | ||
thumbnailUrl | ||
} | ||
} | ||
} | ||
As a result of the query above you will get a list of objects with `__typename` and `id` fields and fields that are specific | ||
to the type of the object. | ||
|
||
.. code-block:: | ||
[ | ||
{ | ||
'__typename': 'Audio', | ||
'id': 1, | ||
'duration': 100, | ||
}, | ||
{ | ||
'__typename': 'Video', | ||
'id': 2, | ||
'thumbnailUrl': 'http://example.com/thumbnail.jpg', | ||
}, | ||
] | ||
Type narrowing | ||
-------------- | ||
|
||
Unlike other graphql implementations `hiku` supports type narrowing without | ||
`__resolveType` function. It is possible because `hiku` knows all possible types | ||
at the link resolution time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<!-- | ||
* Copyright (c) 2021 GraphQL Contributors | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
--> | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>GraphiQL</title> | ||
<style> | ||
body { | ||
height: 100%; | ||
margin: 0; | ||
width: 100%; | ||
overflow: hidden; | ||
} | ||
|
||
#graphiql { | ||
height: 100vh; | ||
} | ||
</style> | ||
|
||
<!-- | ||
This GraphiQL example depends on Promise and fetch, which are available in | ||
modern browsers, but can be "polyfilled" for older browsers. | ||
GraphiQL itself depends on React DOM. | ||
If you do not want to rely on a CDN, you can host these files locally or | ||
include them directly in your favored resource bundler. | ||
--> | ||
<script | ||
crossorigin | ||
src="https://unpkg.com/react@18/umd/react.development.js" | ||
></script> | ||
<script | ||
crossorigin | ||
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" | ||
></script> | ||
|
||
<!-- | ||
These two files can be found in the npm module, however you may wish to | ||
copy them directly into your environment, or perhaps include them in your | ||
favored resource bundler. | ||
--> | ||
<link rel="stylesheet" href="https://unpkg.com/graphiql/graphiql.min.css" /> | ||
</head> | ||
|
||
<body> | ||
<div id="graphiql">Loading...</div> | ||
<script | ||
src="https://unpkg.com/graphiql/graphiql.min.js" | ||
type="application/javascript" | ||
></script> | ||
<script> | ||
const root = ReactDOM.createRoot(document.getElementById('graphiql')); | ||
root.render( | ||
React.createElement(GraphiQL, { | ||
fetcher: GraphiQL.createFetcher({ | ||
url: 'http://localhost:5000/graphql', | ||
}), | ||
defaultEditorToolsVisibility: true, | ||
}), | ||
); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.