Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3477 from trufflesuite/db-resources-filter
Browse files Browse the repository at this point in the history
Allow querying @truffle/db for resources given a list of IDs
  • Loading branch information
gnidan authored Oct 28, 2020
2 parents 5d99849 + 8d11928 commit 2c7230f
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions packages/db/src/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class DefinitionsSchema<C extends Collections> {
id: ID!
}
input QueryFilter {
ids: [ID!]
}
type Query
type Mutation
`;
Expand Down Expand Up @@ -174,7 +178,7 @@ abstract class DefinitionSchema<
${typeDefs}
extend type Query {
${resources}: [${Resource}]
${resources}(filter: QueryFilter): [${Resource}]
${resource}(id: ID!): ${Resource}
}
Expand Down Expand Up @@ -205,6 +209,7 @@ abstract class DefinitionSchema<
// setup loggers for specific resolvers
const logGet = log.extend("get");
const logAll = log.extend("all");
const logFilter = log.extend("filter");

const { resource, resources } = this.names;

Expand All @@ -225,13 +230,26 @@ abstract class DefinitionSchema<
}
},
[resources]: {
resolve: async (_, {}, { workspace }) => {
logAll("Fetching all...");

const result = await workspace.all(resources);

logAll("Fetched all.");
return result;
resolve: async (_, { filter }, { workspace }) => {
if (filter) {
logFilter("Filtering for ids: %o...", filter.ids);

const result = await workspace.find(resources, {
selector: {
id: { $in: filter.ids }
}
})

logFilter("Filtered for ids: %o", filter.ids);
return result;
} else {
logAll("Fetching all...");

const result = await workspace.all(resources);

logAll("Fetched all.");
return result;
}
}
}
}
Expand Down

0 comments on commit 2c7230f

Please sign in to comment.