|
@@ -1,84 +1,121 @@
|
|
|
-import { GraphQLSchema, GraphQLObjectType, GraphQLString } from "graphql";
|
|
|
+import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLInputObjectType, GraphQLInt, GraphQLFloat, GraphQLID, GraphQLList } from "graphql";
|
|
|
import Db from "../../data-storage/db";
|
|
|
import ExtrDb from "../../data-storage/db/extr";
|
|
|
|
|
|
-import { Chileautos } from "../../entities";
|
|
|
-
|
|
|
import { allExtr, getExtr } from "../../use-cases/extr";
|
|
|
|
|
|
+const ExtrSchema = new GraphQLObjectType({
|
|
|
+ name: "Extr",
|
|
|
+ fields: {
|
|
|
+ year: { type: GraphQLInt },
|
|
|
+ maker: { type: GraphQLString },
|
|
|
+ model: { type: GraphQLString },
|
|
|
+ version: { type: GraphQLString },
|
|
|
+ color: { type: GraphQLString },
|
|
|
+ price: { type: GraphQLFloat },
|
|
|
+ title: { type: GraphQLString },
|
|
|
+ description: { type: GraphQLString },
|
|
|
+ plate: { type: GraphQLString },
|
|
|
+ mileage: { type: GraphQLFloat },
|
|
|
+ gearbox: { type: GraphQLString },
|
|
|
+ engine: { type: GraphQLString },
|
|
|
+ fuel: { type: GraphQLString },
|
|
|
+ imageUrl: { type: GraphQLString },
|
|
|
+ code: { type: GraphQLID },
|
|
|
+ url: { type: GraphQLString },
|
|
|
+ region: { type: GraphQLString },
|
|
|
+ city: { type: GraphQLString },
|
|
|
+ sellerType: { type: GraphQLString },
|
|
|
+ sellerName: { type: GraphQLString },
|
|
|
+ publicationCreationDate: { type: GraphQLString },
|
|
|
+ publicationCreationRawDate: { type: GraphQLString },
|
|
|
+ publicationUpdateDate: { type: GraphQLString },
|
|
|
+ publicationCreatedAt: { type: GraphQLString },
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+const ExtrExtSchema = new GraphQLObjectType({
|
|
|
+ name: "ExtrExt",
|
|
|
+ fields: {
|
|
|
+ Extrs: { type: new GraphQLList(ExtrSchema) },
|
|
|
+ Total: { type: GraphQLInt }
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+const Filter = new GraphQLInputObjectType({
|
|
|
+ name: "Filter",
|
|
|
+ fields: {
|
|
|
+ year: { type: GraphQLInt },
|
|
|
+ maker: { type: GraphQLString },
|
|
|
+ model: { type: GraphQLString },
|
|
|
+ version: { type: GraphQLString },
|
|
|
+ color: { type: GraphQLString },
|
|
|
+ price: { type: GraphQLFloat },
|
|
|
+ title: { type: GraphQLString },
|
|
|
+ description: { type: GraphQLString },
|
|
|
+ plate: { type: GraphQLString },
|
|
|
+ mileage: { type: GraphQLFloat },
|
|
|
+ gearbox: { type: GraphQLString },
|
|
|
+ engine: { type: GraphQLString },
|
|
|
+ fuel: { type: GraphQLString },
|
|
|
+ imageUrl: { type: GraphQLString },
|
|
|
+ code: { type: GraphQLID },
|
|
|
+ url: { type: GraphQLString },
|
|
|
+ region: { type: GraphQLString },
|
|
|
+ city: { type: GraphQLString },
|
|
|
+ sellerType: { type: GraphQLString },
|
|
|
+ sellerName: { type: GraphQLString },
|
|
|
+ publicationCreationDate: { type: GraphQLString },
|
|
|
+ publicationCreationRawDate: { type: GraphQLString },
|
|
|
+ publicationUpdateDate: { type: GraphQLString },
|
|
|
+ publicationCreatedAt: { type: GraphQLString },
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
const schema = new GraphQLSchema({
|
|
|
query: new GraphQLObjectType({
|
|
|
- name: "Query",
|
|
|
+ name: "Extrs",
|
|
|
fields: {
|
|
|
- code: {
|
|
|
- type: GraphQLString,
|
|
|
- resolve(rootValue) {
|
|
|
- return "Hello World";
|
|
|
+ getExtr: {
|
|
|
+ type: ExtrSchema,
|
|
|
+ args: {
|
|
|
+ code: { type: GraphQLID },
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
+ allExtrs: {
|
|
|
+ type: ExtrExtSchema,
|
|
|
+ args: {
|
|
|
+ filters: {
|
|
|
+ type: Filter,
|
|
|
+ defaultValue: {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
}
|
|
|
})
|
|
|
-})
|
|
|
-// (`
|
|
|
-// type RandomDie {
|
|
|
-// numSides: Int!
|
|
|
-// rollOnce: Int!
|
|
|
-// roll(numRolls: Int!): [Int]
|
|
|
-// }
|
|
|
-
|
|
|
-// type Extr {
|
|
|
-// year: Int!
|
|
|
-// maker: String!
|
|
|
-// model: String!
|
|
|
-// version: String
|
|
|
-// color: String
|
|
|
-// price: Float!
|
|
|
-// title: String!
|
|
|
-// description: String
|
|
|
-// plate: String
|
|
|
-// mileage: Float
|
|
|
-// gearbox: String
|
|
|
-// engine: String
|
|
|
-// fuel: String
|
|
|
-// imageUrl: String
|
|
|
-// code: String!
|
|
|
-// url: String!
|
|
|
-// region: String
|
|
|
-// city: String
|
|
|
-// sellerType: String
|
|
|
-// sellerName: String
|
|
|
-// publicationCreationDate: String!
|
|
|
-// publicationUpdateDate: String!
|
|
|
-// publicationCreatedAt: String!
|
|
|
-// }
|
|
|
-
|
|
|
-// type Query {
|
|
|
-// hello: String
|
|
|
-// rollDice(numDice: Int!, numSides: Int): [Int]
|
|
|
-// getExtrs(code: String): [Extr]
|
|
|
-// getExtr(code: String): Extr
|
|
|
-// }
|
|
|
-// `);
|
|
|
+});
|
|
|
|
|
|
const tblName = "chileautos";
|
|
|
|
|
|
const db = new Db();
|
|
|
const extrDb = new ExtrDb(db, tblName);
|
|
|
|
|
|
+
|
|
|
const root = {
|
|
|
- hello: () => "hello",
|
|
|
- rollDice: (args: any) => {
|
|
|
- const output = [];
|
|
|
- for (let i = 0; i < args.numDice; i++) {
|
|
|
- output.push(1 + Math.floor(Math.random() * (args.numSides || 6)));
|
|
|
- }
|
|
|
- return output;
|
|
|
- },
|
|
|
- getExtrs: ({ code }: any) => {
|
|
|
- return code ? allExtr.filteredBy({ code }, extrDb) : allExtr.get(extrDb);
|
|
|
+ getExtr: ({ code }: any, __: any, info: any) => {
|
|
|
+ const requestedFields = info.fieldNodes[0].selectionSet.selections.map((field:any) => field.name.value);
|
|
|
+ return getExtr.byCode(code, requestedFields, extrDb);
|
|
|
},
|
|
|
- getExtr: ({ id }: any) => {
|
|
|
- return getExtr.byCode(id, extrDb);
|
|
|
+ allExtrs: async (inputs: any, _: any, info: any) => {
|
|
|
+ console.log("Inputs", inputs);
|
|
|
+
|
|
|
+ const filteredFields = info.fieldNodes[0].selectionSet.selections.filter((field: any) => field.kind === "Field" && field.name.value === "Extrs")[0]
|
|
|
+ const requestedFields = filteredFields.selectionSet.selections.map((field:any) => field.name.value);
|
|
|
+ console.log("Requested Fields", requestedFields);
|
|
|
+
|
|
|
+ const results = inputs.filters ? await allExtr.filteredBy(inputs.filters, requestedFields, extrDb) : await allExtr.get(requestedFields, extrDb);
|
|
|
+ return { Extrs: results, Total: results.length };
|
|
|
}
|
|
|
};
|
|
|
|