debian-mirror-gitlab/doc/development/fe_guide/graphql.md

125 lines
3.5 KiB
Markdown
Raw Normal View History

2019-02-15 15:39:39 +05:30
# GraphQL
We use [Apollo] and [Vue Apollo][vue-apollo] for working with GraphQL
on the frontend.
In order to use GraphQL, you need to enable the `graphql` feature flag,
read more about [Feature Flags][feature-flags].
## Apollo Client
To save duplicated clients getting created in different apps, we have a
2019-07-07 11:18:12 +05:30
[default client][default-client] that should be used. This setups the
2019-02-15 15:39:39 +05:30
Apollo client with the correct URL and also sets the CSRF headers.
## GraphQL Queries
To save query compilation at runtime, webpack can directly import `.graphql`
files. This allows webpack to preprocess the query at compile time instead
of the client doing compilation of queries.
2019-09-04 21:01:54 +05:30
To distinguish queries from mutations and fragments, the following naming convention is recommended:
- `allUsers.query.graphql` for queries;
- `addUser.mutation.graphql` for mutations;
- `basicUser.fragment.graphql` for fragments.
2019-02-15 15:39:39 +05:30
## Usage in Vue
To use Vue Apollo, import the [Vue Apollo][vue-apollo] plugin as well
as the default client. This should be created at the same point
the Vue application is mounted.
```javascript
import Vue from 'vue';
import VueApollo from 'vue-apollo';
2019-07-07 11:18:12 +05:30
import createDefaultClient from '~/lib/graphql';
2019-02-15 15:39:39 +05:30
Vue.use(VueApollo);
const apolloProvider = new VueApollo({
2019-07-07 11:18:12 +05:30
defaultClient: createDefaultClient(),
2019-02-15 15:39:39 +05:30
});
new Vue({
...,
apolloProvider,
...
});
```
2019-10-12 21:52:04 +05:30
Read more about [Vue Apollo][vue-apollo] in the [Vue Apollo documentation](https://vue-apollo.netlify.com/guide/).
2019-02-15 15:39:39 +05:30
2019-09-04 21:01:54 +05:30
### Local state with Apollo
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
It is possible to manage an application state with Apollo by passing
2019-07-07 11:18:12 +05:30
in a resolvers object when creating the default client. The default state can be set by writing
to the cache after setting up the default client.
```javascript
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createDefaultClient from '~/lib/graphql';
Vue.use(VueApollo);
const defaultClient = createDefaultClient({
Query: {
...
},
Mutations: {
...
},
});
defaultClient.cache.writeData({
data: {
isLoading: true,
},
});
const apolloProvider = new VueApollo({
defaultClient,
});
```
2019-09-04 21:01:54 +05:30
Read more about local state management with Apollo in the [Vue Apollo documentation](https://vue-apollo.netlify.com/guide/local-state.html#local-state).
2019-02-15 15:39:39 +05:30
### Testing
With [Vue test utils][vue-test-utils] it is easy to quickly test components that
fetch GraphQL queries. The simplest way is to use `shallowMount` and then set
the data on the component
```javascript
it('tests apollo component', () => {
const vm = shallowMount(App);
vm.setData({
...mock data
});
});
```
2019-09-04 21:01:54 +05:30
Another possible way is testing queries with mocked GraphQL schema. Read more about this way in [Vue Apollo testing documentation](https://vue-apollo.netlify.com/guide/testing.html#tests-with-mocked-graqhql-schema)
2019-02-15 15:39:39 +05:30
## Usage outside of Vue
It is also possible to use GraphQL outside of Vue by directly importing
and using the default client with queries.
```javascript
import defaultClient from '~/lib/graphql';
import query from './query.graphql';
defaultClient.query(query)
.then(result => console.log(result));
```
2019-09-30 21:07:59 +05:30
Read more about the [Apollo] client in the [Apollo documentation](https://www.apollographql.com/docs/tutorial/client/).
2019-02-15 15:39:39 +05:30
[Apollo]: https://www.apollographql.com/
[vue-apollo]: https://github.com/Akryum/vue-apollo/
[feature-flags]: ../feature_flags.md
2019-12-04 20:38:33 +05:30
[default-client]: https://gitlab.com/gitlab-org/gitlab-foss/blob/master/app/assets/javascripts/lib/graphql.js
2019-02-15 15:39:39 +05:30
[vue-test-utils]: https://vue-test-utils.vuejs.org/
2019-07-07 11:18:12 +05:30
[apollo-link-state]: https://www.apollographql.com/docs/link/links/state.html