Skip to content

Commit

Permalink
feat: get many reference
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiocro authored and macrozone committed May 1, 2020
1 parent dbd8e78 commit 6d9c427
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/dataprovider/generated/nexus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export interface NexusGenArgTypes {
first?: number | null; // Int
last?: number | null; // Int
skip?: number | null; // Int
where?: NexusGenInputs['UserRoleWhereInput'] | null; // UserRoleWhereInput
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dataprovider/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type User {
gender: Gender
id: String!
lastName: String
roles(after: UserRoleWhereUniqueInput, before: UserRoleWhereUniqueInput, first: Int, last: Int, skip: Int): [UserRole!]!
roles(after: UserRoleWhereUniqueInput, before: UserRoleWhereUniqueInput, first: Int, last: Int, skip: Int, where: UserRoleWhereInput): [UserRole!]!
yearOfBirth: Int
}

Expand Down
15 changes: 10 additions & 5 deletions packages/dataprovider/src/buildVariables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe("buildVariables", () => {
});

describe("GET_MANY", () => {
it("returns correct variables", () => {
it("returns correct variables for many relation", () => {
const params = {
ids: ["einstein", "rosen", "penrose"],
};
Expand All @@ -213,10 +213,12 @@ describe("buildVariables", () => {
});

describe("GET_MANY_REFERENCE", () => {
it("returns correct variables", () => {
it("returns correct variables for many relation reference", () => {
const params = {
target: "author.id",
id: "author1",
target: "roles",
id: "roleId",
pagination: { page: 10, perPage: 10 },
sort: { field: "email", order: "ASC" },
};

expect(
Expand All @@ -226,7 +228,10 @@ describe("buildVariables", () => {
params,
),
).toEqual<NexusGenArgTypes["Query"]["users"]>({
where: { id: { in: ["author1"] } },
skip: 90,
first: 10,
orderBy: { email: "asc" },
where: { roles: { some: { id: { equals: "roleId" } } } },
});
});
});
Expand Down
28 changes: 14 additions & 14 deletions packages/dataprovider/src/buildVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
IntrospectionInputTypeRef,
IntrospectionInputValue,
IntrospectionNamedTypeRef,
IntrospectionObjectType,
IntrospectionTypeRef,
} from "graphql";
import { IntrospectionResult, Resource } from "./constants/interfaces";

Expand Down Expand Up @@ -256,14 +258,15 @@ const buildNewInputValue = (

const variables = fieldData.reduce<UpdateManyInput>(
(inputs, referencedField) => {
if (isObjectWithId(referencedField)) {
if (!updateListInputType) {
throw new Error(
`Input data for "${fieldName}" is of type "Object" but graphql endpoints does not expose the "update" mutation for "${fullFieldObjectType.name}"`,
);
}
if (isObject(referencedField)) {
// TODO: we assume "data.id" to be the id
if (referencedField.id) {
if (isObjectWithId(referencedField)) {
if (!updateListInputType) {
throw new Error(
`Input data for "${fieldName}" is of type "Object" but graphql endpoints does not expose the "update" mutation for "${fullFieldObjectType.name}"`,
);
}

// update
const data = buildData(
updateListInputType,
Expand Down Expand Up @@ -504,13 +507,10 @@ export default (introspectionResults: IntrospectionResult) => (
},
};
case GET_MANY_REFERENCE: {
return {
where: {
id: {
in: [params.id], // TOOD: not sure if this is correct, looks wrong to me
},
},
};
return buildGetListVariables(introspectionResults)(resource, GET_LIST, {
...params,
filter: { [params.target]: params.id },
});
}
case GET_ONE:
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/dataprovider/test-data/testSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const User = objectType({
t.model.firstName();
t.model.lastName();
t.model.yearOfBirth();
t.model.roles(null);
t.model.roles({ filtering: true });
t.model.gender();
},
});
Expand Down
3 changes: 2 additions & 1 deletion packages/dataprovider/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"outDir": "lib-es"
},
"include": ["src"]
"include": ["src"],
"exclude": ["src/**/*.test.ts", "src/testUtils", "test-data", "generated"]
}

0 comments on commit 6d9c427

Please sign in to comment.