Firestore Apollo GraphQL 项目教程

随笔19小时前发布 北湘西北
3 0 0

Firestore Apollo GraphQL 项目教程

firestore-apollo-graphqlAn example of a GraphQL setup with a Firebase Firestore backend. Uses Apollo Engine/Server 2.0 and deployed to Google App Engine.项目地址:https://gitcode.com/gh_mirrors/fi/firestore-apollo-graphql

1. 项目的目录结构及介绍




firestore-apollo-graphql/


├── src/


│   ├── index.ts


│   ├── datasources/


│   ├── resolvers/


│   ├── schema/


│   ├── types/


├── tests/


├── .gcloudignore


├── .gitignore


├── LICENSE


├── README.md


├── app.yaml


├── package-lock.json


├── package.json


├── tsconfig.json


├── tslint.json

目录结构介绍

src/: 包含项目的主要源代码。
index.ts: 项目的入口文件。datasources/: 包含与Firestore交互的数据源。resolvers/: 包含GraphQL的解析器。schema/: 包含GraphQL的模式定义。types/: 包含TypeScript的类型定义。 tests/: 包含项目的测试文件。.gcloudignore: 用于Google Cloud的忽略文件。.gitignore: 用于Git的忽略文件。LICENSE: 项目的许可证文件。README.md: 项目的说明文档。app.yaml: 用于Google App Engine的配置文件。package-lock.json: 锁定依赖版本的文件。package.json: 项目的依赖和脚本配置文件。tsconfig.json: TypeScript的配置文件。tslint.json: TSLint的配置文件。

2. 项目的启动文件介绍

src/index.ts

这是项目的入口文件,负责启动Apollo Server并连接到Firestore。主要功能包括:

导入必要的模块和配置。初始化Firestore数据源。定义GraphQL的解析器和模式。启动Apollo Server。




import { ApolloServer } from 'apollo-server';


import { typeDefs } from './schema';


import { resolvers } from './resolvers';


import { FirestoreDataSource } from './datasources';


import admin from 'firebase-admin';


 


admin.initializeApp({


  credential: admin.credential.applicationDefault(),


  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'


});


 


const server = new ApolloServer({


  typeDefs,


  resolvers,


  dataSources: () => ({


    firestoreDataSource: new FirestoreDataSource(admin.firestore())


  })


});


 


server.listen().then(({ url }) => {


  console.log(`🚀 Server ready at ${url}`);


});

3. 项目的配置文件介绍

app.yaml

这是用于Google App Engine的配置文件,定义了应用的运行环境和资源配置。




runtime: nodejs14


 


env_variables:


  FIRESTORE_PROJECT_ID: <YOUR_PROJECT_ID>


  FIRESTORE_PRIVATE_KEY: <YOUR_PRIVATE_KEY>


  FIRESTORE_CLIENT_EMAIL: <YOUR_CLIENT_EMAIL>

package.json

这是项目的依赖和脚本配置文件,定义了项目所需的npm包和一些脚本命令。




{


  "name": "firestore-apollo-graphql",


  "version": "1.0.0",


  "main": "src/index.ts",


  "scripts": {


    "start": "ts-node src/index.ts",


    "build": "tsc",


    "test": "mocha -r ts-node/register tests/**/*.test.ts"


  },


  "dependencies": {


    "apollo-server": "^3.0.0",


    "firebase-admin": "^9.0.0",


    "graphql": "^15.0.0"


  },


  "devDependencies": {


    "ts-node": "^10.0.0",


    "typescript": "^4.0.0",


    "mocha": "^8.0.0",


    "tslint": "^6.0.0"


  }


}

tsconfig.json

这是TypeScript的配置文件,定义了TypeScript编译器的选项。




{


  "compilerOptions": {


    "

firestore-apollo-graphqlAn example of a GraphQL setup with a Firebase Firestore backend. Uses Apollo Engine/Server 2.0 and deployed to Google App Engine.项目地址:https://gitcode.com/gh_mirrors/fi/firestore-apollo-graphql

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...