import merge from "lodash/merge" ; import buildRaGraphqlDataProvider from "ra-data-graphql" ; import { DataProvider , DELETE , DELETE_MANY , LegacyDataProvider , UPDATE , UPDATE_MANY , } from "react-admin" ; import { buildQueryFactory } from "./buildQuery" ; import { Options , OurOptions } from "./types" ; import { makeIntrospectionOptions } from "./utils/makeIntrospectionOptions" ; export const defaultOurOptions = { queryDialect : "nexus-prisma" , } ; export const defaultOptions = { clientOptions : { uri : "/graphql" } , ... defaultOurOptions , } ; const buildDataProvider = ( options ) : Promise < LegacyDataProvider > => { const fullOptions = merge ( { } , defaultOptions , options ) ; return buildRaGraphqlDataProvider ( merge ( { } , { buildQuery : buildQueryFactory , introspection : makeIntrospectionOptions ( fullOptions ) , } , fullOptions ) ) . then ( ( graphQLDataProvider ) => { return ( fetchType , resource , params ) : Promise < any > => { if ( fetchType === DELETE_MANY ) { const { ids , ... otherParams } = params ; return Promise . all ( params . ids . map ( ( id ) => graphQLDataProvider ( DELETE , resource , { id , ... otherParams , } ) ) ) . then ( ( results ) => { return { data : results . map ( ( { data } ) => data . id ) } ; } ) ; } if ( fetchType === UPDATE_MANY ) { const { ids , ... otherParams } = params ; return Promise . all ( params . ids . map ( ( id ) => graphQLDataProvider ( UPDATE , resource , { id , ... otherParams , } ) ) ) . then ( ( results ) => { return { data : results . map ( ( { data } ) => data . id ) } ; } ) ; } return graphQLDataProvider ( fetchType , resource , params ) ; } ; } ) ; } ; export default buildDataProvider ;