Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize apollo-utilities graphql transformation functionality #4233

Merged
merged 28 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e8482a2
chore(deps): update dependency @types/graphql to v14
renovate-bot Dec 12, 2018
060f409
Merge branch 'master' into renovate/graphql-14.x
hwillson Dec 12, 2018
d81f876
Set new selection during FieldNode creation
hwillson Dec 17, 2018
76ff754
Convert `GraphQLError[]` use to `ReadonlyArray<GraphQLError>`
hwillson Dec 17, 2018
61fa175
Add `es7` lib to the typescript config
hwillson Dec 17, 2018
5164134
Rewrite transformation utils that were writing into the graphql AST
hwillson Dec 17, 2018
c7083c5
Merge branch 'master' into hwillson/graphql-updates
hwillson Dec 17, 2018
30b7b31
Remove `includes` and tsconfig `es7` use
hwillson Dec 20, 2018
67efcaa
Pull `visit` in via a direct nested import
hwillson Dec 20, 2018
f4e064e
Code cleanup to address outstanding code review comments
hwillson Dec 20, 2018
23bd862
Merge branch 'master' into hwillson/graphql-updates
hwillson Dec 20, 2018
aea4d3d
Replace isNotEmpty helper with simpler isEmpty helper.
benjamn Dec 20, 2018
8ac5d2d
Use boolean expression in directiveMatcher.
benjamn Dec 20, 2018
7eae13d
Use boolean expression in getArgumentMatcher.
benjamn Dec 20, 2018
760297d
Reuse same enter function in removeFragmentSpreadFromDocument.
benjamn Dec 20, 2018
7d17240
Reduce array creation in getAllFragmentSpreadsFromSelectionSet.
benjamn Dec 20, 2018
2a6e7b7
Simplify hasArgumentsInSelection.
benjamn Dec 20, 2018
aad9e9a
Eliminate unused hasArgumentsInSelection[Set] helper functions.
benjamn Dec 20, 2018
af76400
Simplify hasDirectivesInSelection helper function.
benjamn Dec 20, 2018
3b51ad5
Inline filterSelectionSet into sole remaining call site.
benjamn Dec 20, 2018
94eab29
Reuse getDirectiveMatcher in removeDirectivesFromDocument.
benjamn Dec 20, 2018
9a67fcc
Avoid unnecessary node.arguments.filter in removeDirectivesFromDocument.
benjamn Dec 20, 2018
0b6bc76
Extract nullIfDocIsEmpty helper function.
benjamn Dec 20, 2018
f3b640f
Use filterInPlace helper in removeDirectivesFromDocument.
benjamn Dec 20, 2018
131acdd
Make checkDocument(doc) return doc.
benjamn Dec 20, 2018
d8ed6c0
Simplify addTypenameToDocument.
benjamn Dec 20, 2018
60a2ddf
Inline variableDefinitions filter expression.
benjamn Dec 20, 2018
2d3df6e
Changelog updates
hwillson Dec 20, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"devDependencies": {
"@octokit/rest": "15.18.1",
"@types/benchmark": "1.0.31",
"@types/graphql": "0.12.7",
"@types/graphql": "14.0.3",
"@types/isomorphic-fetch": "0.0.34",
"@types/jest": "23.3.10",
"@types/lodash": "4.14.119",
Expand All @@ -105,7 +105,7 @@
"danger": "4.4.10",
"fetch-mock": "7.2.5",
"flow-bin": "0.88.0",
"graphql": "0.13.2",
"graphql": "14.0.2",
"graphql-tag": "2.10.0",
"isomorphic-fetch": "2.2.1",
"jest": "23.6.0",
Expand Down
10 changes: 3 additions & 7 deletions packages/apollo-cache/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,17 @@ function selectionSetFromObj(obj: any): SelectionSetNode {
const selections: FieldNode[] = [];

Object.keys(obj).forEach(key => {
const nestedSelSet: SelectionSetNode = selectionSetFromObj(obj[key]);

const field: FieldNode = {
kind: 'Field',
name: {
kind: 'Name',
value: key,
},
selectionSet: nestedSelSet || undefined,
};

// Recurse
const nestedSelSet: SelectionSetNode = selectionSetFromObj(obj[key]);

if (nestedSelSet) {
field.selectionSet = nestedSelSet;
}

selections.push(field);
});

Expand Down
24 changes: 14 additions & 10 deletions packages/apollo-client/src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { QueryStoreValue } from '../data/queries';

export type ApolloCurrentResult<T> = {
data: T | {};
errors?: GraphQLError[];
errors?: ReadonlyArray<GraphQLError>;
hwillson marked this conversation as resolved.
Show resolved Hide resolved
loading: boolean;
networkStatus: NetworkStatus;
error?: ApolloError;
Expand Down Expand Up @@ -228,7 +228,8 @@ export class ObservableQuery<
public isDifferentFromLastResult(newResult: ApolloQueryResult<TData>) {
const { lastResultSnapshot: snapshot } = this;
return !(
snapshot && newResult &&
snapshot &&
newResult &&
snapshot.networkStatus === newResult.networkStatus &&
snapshot.stale === newResult.stale &&
isEqual(snapshot.data, newResult.data)
Expand Down Expand Up @@ -356,7 +357,9 @@ export class ObservableQuery<
// XXX the subscription variables are separate from the query variables.
// if you want to update subscription variables, right now you have to do that separately,
// and you can only do it by stopping the subscription and then subscribing again with new variables.
public subscribeToMore<TSubscriptionData = TData>(options: SubscribeToMoreOptions<TData, TVariables, TSubscriptionData>) {
public subscribeToMore<TSubscriptionData = TData>(
options: SubscribeToMoreOptions<TData, TVariables, TSubscriptionData>,
) {
const subscription = this.queryManager
.startGraphQLSubscription({
query: options.document,
Expand All @@ -366,13 +369,14 @@ export class ObservableQuery<
next: (subscriptionData: { data: TSubscriptionData }) => {
if (options.updateQuery) {
this.updateQuery((previous, { variables }) =>
(options.updateQuery as UpdateQueryFn<TData, TVariables, TSubscriptionData>)(
previous,
{
subscriptionData,
variables,
},
),
(options.updateQuery as UpdateQueryFn<
TData,
TVariables,
TSubscriptionData
>)(previous, {
subscriptionData,
variables,
}),
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-client/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type PureQueryOptions = {

export type ApolloQueryResult<T> = {
data: T;
errors?: GraphQLError[];
errors?: ReadonlyArray<GraphQLError>;
loading: boolean;
networkStatus: NetworkStatus;
stale: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-client/src/data/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type QueryStoreValue = {
previousVariables?: Object | null;
networkStatus: NetworkStatus;
networkError?: Error | null;
graphQLErrors?: GraphQLError[];
graphQLErrors?: ReadonlyArray<GraphQLError>;
metadata: any;
};

Expand Down Expand Up @@ -78,7 +78,7 @@ export class QueryStore {
networkStatus = NetworkStatus.loading;
}

let graphQLErrors: GraphQLError[] = [];
let graphQLErrors: ReadonlyArray<GraphQLError> = [];
if (previousQuery && previousQuery.graphQLErrors) {
graphQLErrors = previousQuery.graphQLErrors;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-client/src/errors/ApolloError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const generateErrorMessage = (err: ApolloError) => {

export class ApolloError extends Error {
public message: string;
public graphQLErrors: GraphQLError[];
public graphQLErrors: ReadonlyArray<GraphQLError>;
public networkError: Error | null;

// An object that can be used to provide some additional information
Expand All @@ -48,7 +48,7 @@ export class ApolloError extends Error {
errorMessage,
extraInfo,
}: {
graphQLErrors?: GraphQLError[];
graphQLErrors?: ReadonlyArray<GraphQLError>;
networkError?: Error | null;
errorMessage?: string;
extraInfo?: any;
Expand Down
6 changes: 2 additions & 4 deletions packages/apollo-utilities/src/__tests__/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ describe('query transforms', () => {
`;
const expectedQueryStr = print(expectedQuery);

expect(expectedQueryStr).toBe(print(newQueryDoc));
expect(print(newQueryDoc)).toBe(expectedQueryStr);
});

it('should not add duplicates', () => {
Expand Down Expand Up @@ -565,7 +565,7 @@ describe('query transforms', () => {
`;
const expectedQueryStr = print(expectedQuery);

expect(expectedQueryStr).toBe(print(newQueryDoc));
expect(print(newQueryDoc)).toBe(expectedQueryStr);
});

it('should not screw up on a FragmentSpread within the query AST', () => {
Expand Down Expand Up @@ -1167,8 +1167,6 @@ describe('getDirectivesFromDocument', () => {
const expected = gql`
fragment client on ClientData {
hi @client
bye @storage
bar
}

query Mixed {
Expand Down
Loading