在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):realm/realm-graphql开源软件地址(OpenSource Url):https://github.com/realm/realm-graphql开源编程语言(OpenSource Language):TypeScript 100.0%开源软件介绍(OpenSource Introduction):DEPRECATEDWith the introduction of MongoDB Realm, this package is deprecated. Please use MongoDB Realm Cloud and the GraphQL API. Realm GraphQL ClientA set of helper methods and classes to make it easier to use the Apollo GraphQL client with the Realm Object Server. Using the ClientThe Realm GraphQL client provides a few helper and convenience API to make it easier to consume the Realm Object Server GraphQL API with the Apollo Client. PrerequisitesAdd the apollo-link, apollo-link-http, apollo-link-ws, and subscriptions-transport-ws packages to your project:
Then, add the Realm GraphQL client package:
Getting StartedAuthenticating the userTo start consuming the GraphQL API, you'll need to login a user: import { Credentials, User } from 'realm-graphql-client';
const credentials = Credentials.usernamePassword('SOME-USERNAME', 'SOME-PASSWORD');
const user = await User.authenticate(credentials, 'http://my-ros-instance:9080'); Other credential providers are supported, such as JWT, Facebook, Google etc. They are all exposed as factories on the After you have your user, you can create a helper config that will handle token refreshes and authentication: import { GraphQLConfig } from 'realm-graphql-client';
const config = await GraphQLConfig.create(
user,
'/~/test'
); Note that each config is created per Realm path, so if you need to query multiple Realms, you'll need to obtain a config instance for each of them. Setting up the ClientOnce you have a config, you can use that to create an Apollo client instance and configure it. The config exposes 4 properties:
Let's look at a small example. First, let's configure the import { HttpLink } from 'apollo-link-http';
import { concat } from 'apollo-link';
const httpLink = concat(
config.authLink,
// Note: if using node.js, you'll need to provide fetch as well.
new HttpLink({ uri: config.httpEndpoint })
); Then, let's configure the websocket link that we'll use for subscriptions: import { WebSocketLink } from 'apollo-link-ws';
// Note: if using node.js, you'll need to provide webSocketImpl as well.
const webSocketLink = new WebSocketLink({
uri: config.webSocketEndpoint,
options: {
connectionParams: config.connectionParams,
}
}); Finally, we need to use split to direct subscriptions to the websocket link and queries and mutations to the http link: import { split } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
webSocketLink,
httpLink,
);
// Finally, create the client
client = new ApolloClient({
link: link,
cache: new InMemoryCache()
}); Using the clientNow that you have configured your client, you can use to access the ROS GraphQL API. QueriesQuerying data is as simple as invoking const query = gql`
query {
companies {
companyId
name
address
}
}
`;
const response = await client.query({
query: query
});
const companies = response.data.companies; For a complete list of supported query operations, refer to the GraphQL Server docs. For a detailed documentation on the Apollo Client query capabilities, refer to the Apollo docs. For a comprehensive documentation on the query language syntax, refer to the Realm JS Query Language docs. Note: Server Realms (used by GraphQL) don't have named backlinks, even if the client Realms had them defined, so you'll need to use the fully qualified backlink syntax, e.g.: {
golfers(query: "@links.Club.golfers.id = 'some-club-id'") {
firstName
lastName
}
} MutationsMutating data happens when you invoke the const mutation = gql`
mutation {
result: addCompany(input: {
companyId: "some-unique-id"
name: "My Amazing Company"
address: "Mars"
}) {
companyId
name
address
}
}
`
const response = await client.mutate({
mutation: mutation
});
const addedCompany = response.data.result; For a complete list of supported mutation operations, refer to the GraphQL Server docs. For a detailed documentation on the Apollo Client query capabilities, refer to the Apollo docs. SubscriptionsSubscribing for changes happens when you invoke the const observable = await client.subscribe({
query: gql`
subscription {
companies${additionalParameters || ''} {
companyId
name
address
}
}
`
});
observable.subscribe({
next(data) {
const companies = data.data.companies;
// Update you UI
},
error(value) {
// Notify the user of the failure
}
}); For a complete list of supported mutation operations, refer to the GraphQL Server docs. For a detailed documentation on the Apollo Client query capabilities, refer to the Apollo docs. Developing the clientCommands for Building, Cleaning, Testing, Linting and WatchingAfter
Debugging with Visual Studio Code
Some AdviceNever use arrow functions on the Say you want to increase the timeout of the function callback in your tests it('should do something', () => {
this.timeout(2000) // this is not the callback function!
}) |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论