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

fix(api): Add @types/graphql dependency #2369

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@aws-amplify/auth": "^1.2.13",
"@aws-amplify/cache": "^1.0.20",
"@aws-amplify/core": "^1.0.20",
"@types/graphql": "0.13.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these types to devDependencies.

"@types/zen-observable": "^0.5.3",
"axios": "^0.17.0",
"graphql": "0.13.0",
Expand Down
16 changes: 9 additions & 7 deletions packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { OperationDefinitionNode, GraphQLError } from 'graphql';
import { OperationDefinitionNode, GraphQLError, DocumentNode, OperationTypeNode } from 'graphql';
import { print } from 'graphql/language/printer';
import { parse } from 'graphql/language/parser';
import * as Observable from 'zen-observable';
import { RestClient as RestClass } from './RestClient';
import Amplify, { ConsoleLogger as Logger, Credentials } from '@aws-amplify/core';
import { GraphQLOptions, GraphQLResult } from './types';
import { GraphQLOptions, GraphQLResult, isDocumentNode } from './types';
import Cache from '@aws-amplify/cache';
import { v4 as uuid } from 'uuid';

Expand Down Expand Up @@ -312,9 +312,12 @@ export default class APIClass {
* to get the operation type
* @param operation
*/
getGraphqlOperationType(operation) {
const doc = parse(operation);
const { definitions: [{ operation: operationType },] } = doc;
getGraphqlOperationType(operation: string | DocumentNode): OperationTypeNode {
const doc = isDocumentNode(operation) ? operation : parse(operation);
const { definitions } = doc;
const [{ operation: operationType = undefined } = {},] = definitions
.filter(d => d.kind === 'OperationDefinition')
.map(d => d as OperationDefinitionNode);

return operationType;
}
Expand All @@ -329,8 +332,7 @@ export default class APIClass {

const query = typeof paramQuery === 'string' ? parse(paramQuery) : parse(print(paramQuery));

const [operationDef = {},] = query.definitions.filter(def => def.kind === 'OperationDefinition');
const { operation: operationType } = operationDef as OperationDefinitionNode;
const operationType = this.getGraphqlOperationType(query);

switch (operationType) {
case 'query':
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface GraphQLOptions {
variables?: object,
}

export const isDocumentNode = (obj: any): obj is DocumentNode => {
return obj && !!(obj as DocumentNode).kind;
};

export interface GraphQLResult {
data?: object,
errors?: [object],
Expand Down
Loading