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

Fixes #30, Fixes #34, Fixes #36, #35

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 0 additions & 9 deletions examples/prisma-ecommerce/src/App.test.js

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"license": "MIT",
"scripts": {
"build": "tsc",
"watch": "tsc --watch"
"watch": "tsc --watch",
"test": "jest"
},
"dependencies": {
"graphql-tag": "^2.6.1",
Expand All @@ -51,8 +52,10 @@
"cross-env": "^5.2.0",
"graphql": "^0.13.2",
"jest": "^23.6.0",
"react": "16.12.0",
"react-dom": "16.12.0",
"react-admin": "^2.3.0",
"rimraf": "^2.6.2",
"typescript": "^3.0.3"
"typescript": "^3.7.5"
}
}
105 changes: 49 additions & 56 deletions src/buildGqlQuery.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {
TypeKind,
parse,
IntrospectionField,
ArgumentNode,
DocumentNode,
IntrospectionType,
FieldNode,
IntrospectionField,
IntrospectionObjectType,
parse,
SelectionNode,
VariableDefinitionNode,
ArgumentNode,
FieldNode
TypeKind,
VariableDefinitionNode
} from 'graphql';
import { QUERY_TYPES } from 'ra-data-graphql';
import { GET_LIST, GET_MANY, GET_MANY_REFERENCE, DELETE } from 'react-admin';
import { DELETE, GET_LIST, GET_MANY, GET_MANY_REFERENCE } from 'react-admin';
import { IntrospectionResult, Resource } from './constants/interfaces';

import * as gqlTypes from './utils/gqlTypes';
import getFinalType from './utils/getFinalType';
import * as gqlTypes from './utils/gqlTypes';
import isList from './utils/isList';
import isRequired from './utils/isRequired';

Expand All @@ -27,56 +25,53 @@ export interface Query {
export const buildFields = (introspectionResults: IntrospectionResult) => (
fields: IntrospectionField[]
): FieldNode[] => {
return fields.reduce(
(acc: FieldNode[], field) => {
const type = getFinalType(field.type);
return fields.reduce((acc: FieldNode[], field) => {
const type = getFinalType(field.type);

if (type.name.startsWith('_')) {
return acc;
}
if (type.name.startsWith('_')) {
return acc;
}

if (type.kind !== TypeKind.OBJECT) {
return [...acc, gqlTypes.field(gqlTypes.name(field.name))];
}
if (type.kind !== TypeKind.OBJECT) {
return [...acc, gqlTypes.field(gqlTypes.name(field.name))];
}

const linkedResource = introspectionResults.resources.find(
r => r.type.name === type.name
);
const linkedResource = introspectionResults.resources.find(
r => r.type.name === type.name
);

if (linkedResource) {
return [
...acc,
gqlTypes.field(gqlTypes.name(field.name), {
selectionSet: gqlTypes.selectionSet([
gqlTypes.field(gqlTypes.name('id'))
])
})
];
}
if (linkedResource) {
return [
...acc,
gqlTypes.field(gqlTypes.name(field.name), {
selectionSet: gqlTypes.selectionSet([
gqlTypes.field(gqlTypes.name('id'))
])
})
];
}

const linkedType = introspectionResults.types.find(
t => t.name === type.name
);
const linkedType = introspectionResults.types.find(
t => t.name === type.name
);

if (linkedType) {
return [
...acc,
gqlTypes.field(gqlTypes.name(field.name), {
selectionSet: gqlTypes.selectionSet(
buildFields(introspectionResults)(
(linkedType as IntrospectionObjectType).fields
)
if (linkedType) {
return [
...acc,
gqlTypes.field(gqlTypes.name(field.name), {
selectionSet: gqlTypes.selectionSet(
buildFields(introspectionResults)(
(linkedType as IntrospectionObjectType).fields
)
})
];
}
)
})
];
}

// NOTE: We might have to handle linked types which are not resources but will have to be careful about
// ending with endless circular dependencies
return acc;
},
[] as FieldNode[]
);
// NOTE: We might have to handle linked types which are not resources but will have to be careful about
// ending with endless circular dependencies
return acc;
}, [] as FieldNode[]);
};

export const getArgType = (arg: IntrospectionField) => {
Expand Down Expand Up @@ -176,14 +171,12 @@ const buildFieldsFromFragment = (
parsedFragment = parse(fragment);
} catch (e) {
throw new Error(
`Invalid fragment given for resource '${resourceName}' and fetchType '${fetchType}' (${
e.message
}).`
`Invalid fragment given for resource '${resourceName}' and fetchType '${fetchType}' (${e.message}).`
);
}
}

return (parsedFragment as any).definitions[0].selectionSet.selections;
return (parsedFragment as any).definitions?.[0].selectionSet.selections;
};

export default (introspectionResults: IntrospectionResult) => (
Expand Down
Loading