本文整理汇总了TypeScript中apollo-client.ApolloClient.mutate方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ApolloClient.mutate方法的具体用法?TypeScript ApolloClient.mutate怎么用?TypeScript ApolloClient.mutate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apollo-client.ApolloClient
的用法示例。
在下文中一共展示了ApolloClient.mutate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: expect
next: ({ data }) => {
count++;
if (count === 1) {
try {
expect({ ...data }).toMatchObject({ count: 0, lastCount: 1 });
} catch (e) {
done.fail(e);
}
client.mutate({ mutation: increment, variables: { amount: 2 } });
}
if (count === 2) {
try {
expect({ ...data }).toMatchObject({ count: 2, lastCount: 1 });
} catch (e) {
done.fail(e);
}
client.mutate({ mutation: decrement, variables: { amount: 1 } });
}
if (count === 3) {
try {
expect({ ...data }).toMatchObject({ count: 1, lastCount: 1 });
} catch (e) {
done.fail(e);
}
done();
}
},
示例2: expect
next: ({ data }) => {
if (watchCount === 0) {
expect(data.count).toEqual(0);
expect({ ...data.user }).toMatchObject({
__typename: 'User',
firstName: 'John',
});
watchCount += 1;
client.mutate({
mutation,
update: (proxy, { data: { updateUser } }) => {
proxy.writeQuery({
query: userQuery,
data: {
user: { ...updateUser },
},
});
},
});
} else {
expect(data.count).toEqual(1);
expect({ ...data.user }).toMatchObject({
__typename: 'User',
firstName: 'Harry',
});
done();
}
},
示例3: it
it('writeDefaults lets you write defaults to the cache after the store is reset', done => {
const mutation = gql`
mutation foo {
foo @client
}
`;
const query = gql`
{
foo @client
}
`;
const cache = new InMemoryCache();
const stateLink = withClientState({
defaults: {
foo: 'bar',
},
resolvers: {
Mutation: {
foo: (_, $, { cache }) => {
cache.writeData({ data: { foo: 'woo' } });
return null;
},
},
},
cache,
});
const client = new ApolloClient({
cache,
link: stateLink,
});
client.onResetStore(stateLink.writeDefaults);
client
.query({ query })
.then(({ data }) => {
expect({ ...data }).toMatchObject({ foo: 'bar' });
})
.catch(done.fail);
client
.mutate({ mutation })
.then(() => client.query({ query }))
.then(({ data }) => {
expect({ ...data }).toMatchObject({ foo: 'woo' });
})
//should be default after this reset call
.then(() => client.resetStore() as Promise<null>)
.then(() => client.query({ query }))
.then(({ data }) => {
expect({ ...data }).toMatchObject({ foo: 'bar' });
done();
})
.catch(done.fail);
});
示例4: it
it(`doesn't overwrite __typename when writing to the cache with an id`, () => {
const query = gql`
{
obj @client {
field {
field2
}
id
}
}
`;
const mutation = gql`
mutation start {
start @client
}
`;
const local = withClientState({
resolvers: {
Mutation: {
start: (_, $, { cache }: { cache: ApolloCacheClient }) => {
cache.writeQuery({
query,
data: {
obj: {
field: { field2: 1, __typename: 'Field' },
id: 'uniqueId',
__typename: 'Object',
},
},
});
cache.writeData({
id: 'Object:uniqueId',
data: { field: { field2: 2, __typename: 'Field' } },
});
return { start: true };
},
},
},
});
const client = new ApolloClient({
cache: new InMemoryCache(),
link: local,
});
return client
.mutate({ mutation })
.then(() => client.query({ query }))
.then(({ data }: any) => {
expect(data.obj.__typename).toEqual('Object');
expect(data.obj.field.__typename).toEqual('Field');
})
.catch(e => console.log(e));
});
示例5: it
it('correctly propagates an error from a client-state resolver', async done => {
const data = {
list: {
__typename: 'List',
items: [
{ __typename: 'ListItem', id: 1, name: 'first', isDone: true },
{ __typename: 'ListItem', id: 2, name: 'second', isDone: false },
],
},
};
// mocked endpoint acting as server data
const http = new ApolloLink(() => Observable.of({ data }));
const local = withClientState({
resolvers: {
Query: {
hasBeenIllegallyTouched: (_, _v, _c) => {
throw new Error('Illegal Query Operation Occurred');
},
},
Mutation: {
touchIllegally: (_, _v, _c) => {
throw new Error('Illegal Mutation Operation Occurred');
},
},
},
});
const client = new ApolloClient({
link: local.concat(http),
cache: new InMemoryCache(),
});
const variables = { id: 1 };
const query = gql`
query hasBeenIllegallyTouched($id: Int!) {
hasBeenIllegallyTouched(id: $id) @client
}
`;
const mutation = gql`
mutation SelectItem($id: Int!) {
touchIllegally(id: $id) @client
}
`;
try {
await client.query({ query, variables });
done.fail('Should have thrown!');
} catch (e) {
// Test Passed!
expect(() => {
throw e;
}).toThrowErrorMatchingSnapshot();
}
try {
await client.mutate({ mutation, variables });
done.fail('Should have thrown!');
} catch (e) {
// Test Passed!
expect(() => {
throw e;
}).toThrowErrorMatchingSnapshot();
}
done();
});